glad-functions
v0.1.0
Published
glad-functions is the library that extends prelude-ls's functional style programing support.
Downloads
35
Keywords
Readme
glad-functions
glad-functions is the library that extends prelude-ls's functional style programing support.
Usage
Applicative
return(return_)
a -> (a -> b)
return_ \something |> (do) # => 'something'
something = \something
something
|> return_ \other_thing # => 'other_shing'
Async
before
(a -> b) -> (c -> d) -> ((c -> d) -> (a -> b))
Extend the function with the other function. When the "parent" is called. the "child" will be called before the "parent". Both of the functions should accept the same type and numbers of arguments.(However, the "child" function should accept "next" as the last argument adding the common arguments)
hello_then = (name, cb)->
cb "Hello_" + name
hello_then_alpha = before hello_then, (name, cb, next)->
next \lorem_ + name, cb
hello_then_alpha \foo, -> it + \_test
|> console~log # =>(Output) Hello_lorem_foo_test
after
(a -> b) -> (c -> d) -> ((a -> b) -> (c -> d))
Extend the function with the other function. When the "parent" is called. the "child" will be called after the "parent". Both of the functions should accept the same type and numbers of arguments.
hello_then = (name, cb)->
cb name + "_is_a"
hello_then_beta = after hello_then, (name, cb)->
cb name + "_good"
hello_then_beta \foo, -> it + \_man # => "foo_is_a_good_man
Combinator
B
Alias: (<<)
C
Alias: flip
I
Alias: id
K
Alias: Applicative.return_
Q
Alias: (>>)
Y
Alias: fix
Control
if(if_), unless(unless_)
Boolean -> (a -> b) -> b?
some_value = 200
if_ (some_value > 100), -> console.log \Yeah! # => (Output) 'Yeah!'
unless_ (some_value > 100), -> console.log \Yeah! # => (Do Nothing)
try(try_), catch(catch_), finally(finally_)
do_something = ->
throw new Error "Some Error!"
try_ do_something, ( .message) >> console~log # => (Output) 'Some Error!'
do_something `catch_` ( .message |> console~log) # => (Output) 'Some Error!'
do_something `finally_` -> console.log "Finally do other things." # => (Output) 'Finally do other things.' (Error)
throw(throw_)
throw_ new Error "Some Error!" # => (Error) 'Some Error!'
Flow
act
(a -> b) -> a -> a
[1 to 5]
|> filter (% 2) >> (is 0)
|> act console~log # => (Output) [ 2, 4 ]
|> filter (> 3)
|> act console~log # => (Output) [ 4 ]
|> map (* 100)
|> console~log # => (Output) [ 400 ]
if(if_), unless(unless_)
Boolean -> (a -> b) -> (a OR b)
SOME_CONSTANT = 200
\Yeah!
|> if_ (SOME_CONSTANT > 100), console~log # => (Output) 'Yeah!'
SOME_CONSTANT = 200
\Yeah!
|> unless_ (SOME_CONSTANT > 100), console~log # => (Do Nothing)
when(when_), except
(a -> Boolean) -> (a -> b) -> (a OR b)
[1 to 5]
|> when_ (all ( < 6)), ( .length) >> console~log # => (Output) 5
[1 to 5]
|> except (any ( is 2)), ( .length) >> console~log # => (Do Nothing)
people =
* name: \tarou, blood: \a, age: 23
* name: \hanako, blood: \b, age: 25
* name: \jirou, blood: \o, age: 24
people
|> each when_ ( .age > 24), ( .name) >> console~log # => (Output) 'hanako'
|> map except ( .blood is \b), ( .name)
|> map ( or \blood_b) # => [ 'tarou', 'blood_b', 'jirou' ]
then(then_), else(else_)
(a -> b) -> Boolean -> (a OR b)
[1 to 5]
|> any ( < 5)
|> act then_ -> console.log \Yeah! # => (Output) 'Yeah!'
|> else_ -> console.log \Oh! # => (Do Nothing)
[1 to 5]
|> all ( < 5)
|> then_ act -> console.log \Yeah! # => (Do Nothing)
|> else_ -> console.log \Oh! # => (Output) 'Oh!'
case(case_), otherwise(otherwise_)
(a -> Boolean) -> (a -> b) -> (a OR c)
c is special unique object
[1 to 3]
|> case_ (all (< 3)), -> console.log \Yeah! # => (Do Nothing)
|> case_ (all (< 4)), -> console.log \Yeah! # => (Output) 'Yeah!'
|> case_ (all (< 5)), -> console.log \Yeah! # => (Do Nothing)
|> otherwise_ -> console.log \Yeah! # => (Do Nothing)
case$
(a -> Boolean) -> (a -> b) -> a
Equivalent to (act . when)
[1 to 3]
|> case$ (all (< 3)), -> console.log \Yeah! # => (Do Nothing)
|> case$ (all (< 4)), -> console.log \Yeah! # => (Output) 'Yeah!'
|> case$ (all (< 5)), -> console.log \Yeah! # => (Output) 'Yeah!'
Func
$
(a -> b) -> a -> b
( + 5) `$` 4 # => 9
[3 5 7]
|> map flip (%)
|> map (<<) (is 0)
|> all $ _, 105
|> then_ ->
console.log \Yeah! # => (Output) 'Yeah!'
$$
[(a -> b), (a -> c), ...] -> a -> [b, c, ...]
Equivalent to flip dist
5 |> $$ [(+ 4), (* 4)] # => [ 9, 20 ]
[1 to 5]
|> $$ [length >> (`div` 2), reverse]
|> apply replicate # => [ [ 5, 4, 3, 2, 1 ], [ 5, 4, 3, 2, 1 ] ]
lazy
((a, b, c, ...) -> d) -> e -> d
lazy (+), 5, 5 # => [Function]
lazy (+), 5, 5 |> (do) # => 10
set-timeout (lazy console~log, \Yeah!), 3000_ms # => (Output in 3 seconds) 'Yeah!'
dist
a -> [(a -> b), (a -> c), ...] -> [b, c, ...]
dist 5, [(+ 4), (* 4)] # => [ 9, 20 ]
[1 to 5]
|> dist _, [length >> (`div` 2), reverse]
|> apply replicate # => [ [ 5, 4, 3, 2, 1 ], [ 5, 4, 3, 2, 1 ] ]
arg
Number -> (a, b, c, ...) -> (a OR b OR c OR ...)
(arg 1) 1, 2, 3 # => 2
express!.get \./, (arg 1) >> let_ \render, \index # second argument of callback is Response
args
(a, b, c, ...) -> [a, b, c, ...]
args 1, 2, 3 # => [ 1, 2, 3 ]
express!.get \./, args >> (++ \foo) >> cb # useful when add argument
withl
(a -> b) -> [b, a]
5 |> withl ( + 10) # => [ 15, 5 ]
[10 to 15]
|> withl elem-index 14
|> apply split-at # => [ [ 10, 11, 12, 13 ], [ 14, 15 ] ]
withr
(a -> b) -> [a, b]
5 |> withr ( + 10) # => [ 5, 15]
$_at
Number -> (a -> b) -> [a] -> [a OR b]
[1, 2, 3]
|> $_at 2, (* 10) # => [ 1, 2, 30 ]
$_zip
[(a -> b), (c -> d), ...] -> [a, b, ...] -> [c, d, ...]
[1, 2, 3]
|> $_zip [(* 10), (- 2), (+ 7)] # => [ 10, 0, 10 ]
$_head
a -> [b] -> [c]
[1, 2, 3]
|> $_head (* 10) # => [10, 2, 3]
$_last
a -> [b] -> [c]
[1, 2, 3]
|> $_last (* 10) # => [1, 2, 30]
$_arg
Number -> (a -> b) -> (c -> d) -> (e -> f)
$_arg 1, (+ 5), (-)
|> apply _, [10, 20] # => -15
$_head_arg
(a -> b) -> (c -> d) -> (e -> f)
$_head_arg (+ 100), (-)
|> apply _, [10, 20] # => 90
$_last_arg
(a -> b) -> (c -> d) -> (e -> f)
$_last_arg (+ 100), (-)
|> apply _, [10, 20] # => -110
$_args
(a -> b) -> (c -> d) -> (e -> f)
$_args (+ 5), (*)
|> apply _, [10, 20] # => 375
$$_args
[(a -> b), ...] -> (c -> d) -> (e -> f)
$$_args [(+ 100), (+ 5)], (*)
|> apply _, [10, 20]# => 2750
$_when
Function -> Function -> [a] -> [a]
[10, 20, 30]
|> $_when (> 20), (* 30) # => [10, 20, 900]
$_find
Function -> Function -> [a] -> [a]
<[foo bar foobar bar foo lorem ]>
|> $_find (is \bar), ( + \barbar)
# => [ 'foo', 'barbarbar', 'foobar', 'bar', 'foo', 'lorem' ]
$_filter
Function -> Function -> [a] -> [a]
Equivalent to
$_when
[10, 20, 30]
|> $_filter (> 10), (* 30) # => [ 10, 600, 900 ]
$_pairs
Function -> Object -> Object
{ ks : 10 , ms : 2 }
|> $_pairs map map (+ \5) # => { ks5: '105', ms5: '25' }
{ ks : 10, ms : 2, mm : 5, kg : 7 }
|> $_pairs filter head >> (in <[ks kg]>) # => { ks: 10, kg: 7 }
$_key
String -> (a -> b) -> c -> d
{ ks : 10 , ms : 2}
|> $_key \ms (+ 5) # => { ks: 10, ms: 7 }
$_any
[Function] -> a -> Boolean
[
* name: \Bob, job: \engineer, age: 28
* name: \Jone, job: \director, age: 35
* name: \Allen, job: \designer, age: 30
]
|> filter $_any [( .job is \engineer), ( .age <= 30)]
# => [{name: \Bob, job: \engineer, age: 28}, {name: \Allen, job: \designer, age: 30}]
$_all
[Function] -> a -> Boolean
[
* name: \Bob, job: \engineer, age: 28
* name: \Jone, job: \director, age: 35
* name: \Allen, job: \designer, age: 30
]
|> filter $_all [( .job is \director), ( .age >= 30)]
# => [{name: \Jone, job: \director, age: 35}]
need
Number -> Function -> Function
(+)
|> need 2
|> apply _, [10]
|> apply _, [4] # => 14
List
find_map
(a -> b) -> [a] -> b
<[foo bar]>
|> find_map match_ /a(r)/
|> at 1 # => 'r'
filter_map
(a -> b) -> [a] -> [b]
<[foo bar]>
|> filter_map match_ /a(r)/
|> (at 0) >> (at 1) # => 'r'
length
[a] -> Number
[1 to 3] |> length # => 3
pick
[Number] -> [a] -> [a]
[1 to 40] |> pick [4, 23, 13, 5, 1] # => [ 5, 24, 14, 6, 2 ]
[Number] -> [a] -> [a]
some_function = (cb)->
cb \err, \data, \other_data
some_function (args >> (pick [2, 1]) >> join " ") # => "other_data data"
list
(a, b, c, ...) -> [a, b, c, ...]
list 1, 2, 3, 4 # => [1, 2, 3, 4]
range
a -> b -> [a]
range 4, 100 # => [4, 5, ... 99]
none
(a → Boolean) → [a] → Boolean
<[foo bar foobar]> |> none (match_ /^foo/) # => false
<[foo bar foobar]> |> none (match_ /^lorem/) # => true
[] |> none (match_ /^lorem/) # => true
Obj
let(let_)
(a, String, b, c, ...) -> d
let_ console, \log, \Yeah! # => (Output) 'Yeah!'
get
(String) -> a -> b
human = name: \tarou
human |> get \name # => 'tarou'
human = name: \tarou, age: 25 weight: 60
<[name age]> |> map get _, human # => [ 'tarou', 25 ]
set
(String) -> a -> b -> a
human = {}
human
|> act set \name, \tarou
|> act set \age, 25 # => { name: 'tarou', age: 25 }
delete(delete_)
(String) -> a -> b
(foo: \bar)
|> act delete_ \foo # => {}
set_$
String -> (a -> b) -> c -> d
{ foo: 4, bar: 5}
|> act set_$ \bar, (+ 8) # => { foo: 4, bar: 13 }
new_
a -> b ... -> c
(class A
(num) ->
@x = num
property: 1
method: (y) ->
@x + @property + y)
|> new_ _, 100
|> _let _, \method, 32 # => 133
call
String -> a ... -> b -> c
call \plus_w, 3, 8 ,(plus_w: ( + ) >> ( * 2 )) # => 22
Option
may
(a -> b) -> a? -> b?
Equivalent to when (?)
people =
* name: \tarou
* name: \hanako
save = (person)-> # Save into database
people
|> find (get \name) >> (is \jirou)
|> may save # => (Do Nothing)
Str
match(match_)
(String OR RegExp) -> String -> [String]?
\foo |> match_ /oo/ |> at 0 # => 'oo'
Other
Module Exports Priority
- Func
- Combinator
- Applicative
- Option
- Flow
- Async
- Control
- List
- Str
- Obj