Skip to content

Commit

Permalink
fixes #363
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Aug 31, 2021
1 parent 4e1cd9e commit ba2ac3c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
3 changes: 3 additions & 0 deletions fastcore/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
"nested_idx": "01_basics.ipynb",
"val2idx": "01_basics.ipynb",
"uniqueify": "01_basics.ipynb",
"loop_first_last": "01_basics.ipynb",
"loop_first": "01_basics.ipynb",
"loop_last": "01_basics.ipynb",
"num_methods": "01_basics.ipynb",
"rnum_methods": "01_basics.ipynb",
"inum_methods": "01_basics.ipynb",
Expand Down
34 changes: 29 additions & 5 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
'delegate_attr', 'ShowPrint', 'Int', 'Str', 'Float', 'concat', 'strcat', 'detuplify', 'replicate', 'setify',
'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', 'filter_keys', 'filter_values', 'cycle',
'zip_cycle', 'sorted_ex', 'not_', 'argwhere', 'filter_ex', 'range_of', 'renumerate', 'first', 'nested_attr',
'nested_idx', 'val2idx', 'uniqueify', 'num_methods', 'rnum_methods', 'inum_methods', 'fastuple', 'arg0',
'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'mapt', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate',
'using_attr', 'Self', 'Self', 'copy_func', 'patch_to', 'patch', 'patch_property', 'compile_re', 'ImportEnum',
'StrEnum', 'str_enum', 'Stateful', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'typed',
'exec_new']
'nested_idx', 'val2idx', 'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'num_methods',
'rnum_methods', 'inum_methods', 'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'mapt', 'map_ex',
'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'Self', 'Self', 'copy_func', 'patch_to',
'patch', 'patch_property', 'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'Stateful', 'PrettyString',
'even_mults', 'num_cpus', 'add_props', 'typed', 'exec_new']

# Cell
from .imports import *
Expand Down Expand Up @@ -576,6 +576,30 @@ def uniqueify(x, sort=False, bidir=False, start=None):
if sort: res.sort()
return (res,val2idx(res)) if bidir else res

# Cell

# looping functions from https://github.com/willmcgugan/rich/blob/master/rich/_loop.py
def loop_first_last(values):
"Iterate and generate a tuple with a flag for first and last value."
iter_values = iter(values)
try: previous_value = next(iter_values)
except StopIteration: return
first = True
for value in iter_values:
yield first,False,previous_value
first,previous_value = False,value
yield first,True,previous_value

# Cell
def loop_first(values):
"Iterate and generate a tuple with a flag for first and last value."
return ((b,o) for b,_,o in loop_first_last(values))

# Cell
def loop_last(values):
"Iterate and generate a tuple with a flag for first and last value."
return ((b,o) for _,b,o in loop_first_last(values))

# Cell
num_methods = """
__add__ __sub__ __mul__ __matmul__ __truediv__ __floordiv__ __mod__ __divmod__ __pow__
Expand Down
81 changes: 79 additions & 2 deletions nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3363,6 +3363,78 @@
"test_eq(o,{0:0, 1: 1, 3: 2, 5: 3})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"\n",
"# looping functions from https://github.com/willmcgugan/rich/blob/master/rich/_loop.py\n",
"def loop_first_last(values):\n",
" \"Iterate and generate a tuple with a flag for first and last value.\"\n",
" iter_values = iter(values)\n",
" try: previous_value = next(iter_values)\n",
" except StopIteration: return\n",
" first = True\n",
" for value in iter_values:\n",
" yield first,False,previous_value\n",
" first,previous_value = False,value\n",
" yield first,True,previous_value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_eq(loop_first_last(range(3)), [(True,False,0), (False,False,1), (False,True,2)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def loop_first(values):\n",
" \"Iterate and generate a tuple with a flag for first and last value.\"\n",
" return ((b,o) for b,_,o in loop_first_last(values))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_eq(loop_first(range(3)), [(True,0), (False,1), (False,2)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def loop_last(values):\n",
" \"Iterate and generate a tuple with a flag for first and last value.\"\n",
" return ((b,o) for _,b,o in loop_first_last(values))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_eq(loop_last(range(3)), [(False,0), (False,1), (True,2)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -3585,8 +3657,13 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Other Elementwise Operations\n",
"\n",
"#### Other Elementwise Operations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Additionally, the following elementwise operations are available:\n",
"- `le`: less than or equal\n",
"- `eq`: equal\n",
Expand Down

0 comments on commit ba2ac3c

Please sign in to comment.