0.1.0 • Published 7 years ago

glad-functions v0.1.0

Weekly downloads
17
License
ISC
Repository
github
Last release
7 years ago

glad-functions

glad-functions is the library that extends prelude-ls's functional style programing support.

npm version Build Status

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

  1. Func
  2. Combinator
  3. Applicative
  4. Option
  5. Flow
  6. Async
  7. Control
  8. List
  9. Str
  10. Obj
0.1.0

7 years ago

0.0.30

8 years ago

0.0.29

8 years ago

0.0.28

8 years ago

0.0.27

8 years ago

0.0.26

8 years ago

0.0.25

8 years ago

0.0.24

8 years ago

0.0.23

8 years ago

0.0.22

8 years ago

0.0.21

8 years ago

0.0.20

8 years ago

0.0.19

8 years ago

0.0.18

8 years ago

0.0.17

8 years ago

0.0.16

8 years ago

0.0.15

8 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago

0.0.0

8 years ago