Module: StdLib

Included in:
F
Defined in:
lib/fubby/std_lib.rb

Instance Method Summary collapse

Instance Method Details

#addObject

add

Int -> Int -> Int



72
73
74
# File 'lib/fubby/std_lib.rb', line 72

def add
  ->(x, y) { x + y }
end

#anyObject

any

(a -> Bool) -> Bool



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fubby/std_lib.rb', line 50

def any
  C.curry.(
    ->(f, x) {
      fx = ->(memo, x) {
        f.(x) ? true : memo
      }

      C.reduce.(fx, x, false)
    }
  )
end

#downcaseObject

TODO implement this without ‘downcase`

downcase

String -> String



104
105
106
# File 'lib/fubby/std_lib.rb', line 104

def downcase
  ->(x) { x.downcase }
end

#f_allObject

any

(a -> Bool) -> Bool



63
64
65
66
67
68
69
# File 'lib/fubby/std_lib.rb', line 63

def f_all
  C.curry.(
    ->(f, x) {
      !any.(->(x) { x == false }, map.(f, x))
    }
  )
end

#headObject

head
a

-> a



77
78
79
# File 'lib/fubby/std_lib.rb', line 77

def head
  ->(x) { x[0] }
end

#lengthObject

length
a

-> Number



12
13
14
15
16
# File 'lib/fubby/std_lib.rb', line 12

def length
  ->(x) {
    C.reduce.(0, ->(memo, _) { memo + 1 }, x)
  }
end

#mapObject

map

(a -> b) -> [a] -> [b]



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fubby/std_lib.rb', line 19

def map
  C.curry.(
    ->(f, x) {
      fx = ->(memo, x) {
        A.concat.(memo, [f.(x)])
      }

      C.reduce.([], fx, x)
    }
  )
end

#rejectObject

reject

(a -> Bool) -> [a] -> [a]



45
46
47
# File 'lib/fubby/std_lib.rb', line 45

def reject
  C.curry.(->(f, x) { x - select.(f, x) })
end

#reverseObject

reverse
a

-> [a]



82
83
84
85
86
87
88
89
90
# File 'lib/fubby/std_lib.rb', line 82

def reverse
  ->(array) {
    if array.empty? || length.(array) == 1
      array
    else
      add.(reverse.(array[1..-1]), array[0])
    end
  }
end

#selectObject

select

(a -> Bool) -> [a] -> [a]



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fubby/std_lib.rb', line 32

def select
  C.curry.(
    ->(f, x) {
      fx = ->(memo, x) {
        f.(x) == true ? A.concat.(memo, [x]) : memo
      }

      C.reduce.([], fx, x)
    }
  )
end

#str_headObject

TODO: figure out how to handle Strings and Chars



98
99
100
# File 'lib/fubby/std_lib.rb', line 98

def str_head
  ->(x) { x[0] }
end

#tailObject

tail
a

-> a



93
94
95
# File 'lib/fubby/std_lib.rb', line 93

def tail
  C.compose.(head, reverse)
end

#upcaseObject

TODO implement this without ‘capitalize`

downcase

String -> String



110
111
112
# File 'lib/fubby/std_lib.rb', line 110

def upcase
  ->(x) { x.capitalize }
end