From cbcbe39212027c9aea913df6d00585421156493c Mon Sep 17 00:00:00 2001 From: inoas Date: Mon, 4 Apr 2022 22:03:19 +0000 Subject: Add tap/1 Shamelessy stolen and adopted from @hayleigh-dot-dev and https://hexdocs.pm/elixir/main/Kernel.html#tap/2 --- src/gleam/function.gleam | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/gleam/function.gleam b/src/gleam/function.gleam index 5aebb76..007eded 100644 --- a/src/gleam/function.gleam +++ b/src/gleam/function.gleam @@ -52,9 +52,18 @@ pub fn identity(x: a) -> a { x } -/// A function that takes a single argument and returns a new function that +/// Takes a single argument and returns a new function that /// ignores its argument and always returns the input value. /// pub fn constant(value: a) -> fn(b) -> a { fn(_) { value } } + +/// Takes a function with a single argument +/// calls that function with that argument +/// and returns that argument instead of the function return value. +/// Useful for running synchronous side effects in a pipeline. +pub fn tap (arg, effect: fn (a) -> b) -> a { + effect(arg) + arg +} -- cgit v1.2.3