Merge pull request #172 from 30-seconds/add-new-snippets

Add function snippets
This commit is contained in:
Angelos Chalaris
2020-01-03 09:09:11 +02:00
committed by GitHub
7 changed files with 161 additions and 0 deletions

23
snippets/check_prop.md Normal file
View File

@ -0,0 +1,23 @@
---
title: function_name
tags: utility,intermediate
---
Given a predicate function, `fn`, and a `prop` string, this curried function will then take an object to inspect by calling the property and passing it to the predicate.
Return a `lambda` function that takes an object and applies the predicate function, `fn` to the specified property.
```py
def check_prop(fn, prop):
return lambda obj: fn(obj[prop])
```
```py
check_age = check_prop(lambda x: x >= 18, 'age')
user = {
'name': 'Mark',
'age': 18
}
check_age(user) # True
```

24
snippets/compose.md Normal file
View File

@ -0,0 +1,24 @@
---
title: compose
tags: function,intermediate
---
Performs right-to-left function composition.
Use `reduce()` to perform right-to-left function composition.
The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.
```py
from functools import reduce
def compose(*fns):
return reduce(lambda f, g: lambda *args: f(g(*args)), fns)
```
```py
add5 = lambda x: x + 5
multiply = lambda x, y: x * y
multiply_and_add_5 = compose(add5, multiply)
multiply_and_add_5(5, 2) # 15
```

24
snippets/compose_right.md Normal file
View File

@ -0,0 +1,24 @@
---
title: compose_right
tags: function,intermediate
---
Performs left-to-right function composition.
Use `reduce()` to perform left-to-right function composition.
The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
```py
from functools import reduce
def compose_right(*fns):
return reduce(lambda f, g: lambda *args: g(f(*args)), fns)
```
```py
add = lambda x, y: x + y
square = lambda x: x * x
add_and_square = compose_right(add,square)
add_and_square(1, 2) # 9
```

22
snippets/curry.md Normal file
View File

@ -0,0 +1,22 @@
---
title: curry
tags: function,intermediate
---
Curries a function.
Use `partial()` to return a new partial object which behaves like `fn` with the given arguments, `args`, partially applied.
```py
from functools import partial
def curry(fn, *args):
return partial(fn,*args)
```
```py
add = lambda x, y: x + y
add10 = curry(sum, 10)
add10(20) # 30
```

24
snippets/delay.md Normal file
View File

@ -0,0 +1,24 @@
---
title: delay
tags: function,intermediate
---
Invokes the provided function after `ms` milliseconds.
Use `sleep()` to delay the execution of `fn` by `ms / 1000` seconds.
```py
from time import sleep
def delay(fn, ms, *args):
sleep(ms / 1000)
return fn(*args)
```
```py
delay(
lambda x: print(x),
1000,
'later'
) # prints 'later' after one second
```

25
snippets/unfold.md Normal file
View File

@ -0,0 +1,25 @@
---
title: unfold
tags: function,list,advanced
---
Builds a list, using an iterator function and an initial seed value.
The iterator function accepts one argument (`seed`) and must always return a list with two elements ([`value`, `nextSeed`]) or `False` to terminate.
Use a generator function, `fn_generator`, that uses a `while` loop to call the iterator function and `yield` the `value` until it returns `False`.
Use list comprehension to return the list that is produced by the generator, using the iterator function.
```py
def unfold(fn, seed):
def fn_generator(val):
while True:
val = fn(val[1])
if val == False: break
yield val[0]
return [i for i in fn_generator([None, seed])]
```
```py
f = lambda n: False if n > 50 else [-n, n + 10]
unfold(f, 10) # [-10, -20, -30, -40, -50]
```

19
snippets/when.md Normal file
View File

@ -0,0 +1,19 @@
---
title: when
tags: function,intermediate
---
Tests a value, `x`, against a `predicate` function, conditionally applying a function.
Check if the value of `predicate(x)` is `True` and if so return `when_true(x)`, otherwise return `x`.
```py
def when(predicate, when_true):
return lambda x: when_true(x) if predicate(x) else x
```
```py
double_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2)
double_even_numbers(2) # 4
double_even_numbers(1) # 1
```