diff --git a/snippets/check_prop.md b/snippets/check_prop.md new file mode 100644 index 000000000..cb98054bf --- /dev/null +++ b/snippets/check_prop.md @@ -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 +``` diff --git a/snippets/compose.md b/snippets/compose.md new file mode 100644 index 000000000..08f90736a --- /dev/null +++ b/snippets/compose.md @@ -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 +``` diff --git a/snippets/compose_right.md b/snippets/compose_right.md new file mode 100644 index 000000000..696f89da6 --- /dev/null +++ b/snippets/compose_right.md @@ -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 +``` diff --git a/snippets/curry.md b/snippets/curry.md new file mode 100644 index 000000000..855cd81a6 --- /dev/null +++ b/snippets/curry.md @@ -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 +``` diff --git a/snippets/delay.md b/snippets/delay.md new file mode 100644 index 000000000..d77d11683 --- /dev/null +++ b/snippets/delay.md @@ -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 +``` diff --git a/snippets/unfold.md b/snippets/unfold.md new file mode 100644 index 000000000..a354cc4a8 --- /dev/null +++ b/snippets/unfold.md @@ -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] +``` diff --git a/snippets/when.md b/snippets/when.md new file mode 100644 index 000000000..e45fac3b1 --- /dev/null +++ b/snippets/when.md @@ -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 +```