class Funcify::Fn

Public Class Methods

add() click to toggle source

add an element to an array

# File lib/funcify/fn.rb, line 42
def add
  -> x, xs { xs << x }.curry
end
all?() click to toggle source
# File lib/funcify/fn.rb, line 71
def all?
  ->(f, enum) { enum.all? { |e| f.(e) } }.curry
end
all_keys() click to toggle source
# File lib/funcify/fn.rb, line 239
def all_keys
  -> h { h.flat_map { |k, v| [k] + (v.is_a?(Hash) ? all_keys.(v) : [v]) } }
end
all_success?() click to toggle source
# File lib/funcify/fn.rb, line 357
def all_success?
  Fn.all?.(Monad.maybe_value_ok?)
end
any?() click to toggle source
# File lib/funcify/fn.rb, line 75
def any?
  ->(f, enum) { enum.any? { |e| f.(e) } }.curry
end
apply() click to toggle source

Apply that takes a function and an enum and applies the fn to the entire enum Works with methods like join, split

# File lib/funcify/fn.rb, line 186
def apply
  ->(f, enum) { f.(enum) }.curry
end
at() click to toggle source

right at; takes the key/index and applies the enum

# File lib/funcify/fn.rb, line 230
def at
  ->(x, i) { i[x] unless i.nil? }.curry
end
break_point() click to toggle source
# File lib/funcify/fn.rb, line 287
def break_point
  -> args { binding.pry }
end
change_set_fn() click to toggle source

f: add fn g: remove fn prev state this state

# File lib/funcify/fn.rb, line 311
def change_set_fn
  -> f, g, prev, this {
    f.(Set.new(this) - Set.new(prev))
    g.(Set.new(prev) - Set.new(this))
  }.curry
end
coherse() click to toggle source
# File lib/funcify/fn.rb, line 243
def coherse
  -> f, xs { map.(-> x { x.send(f) } ).(xs) }.curry
end
compose() click to toggle source

the famous compose Applies from right to left, taking the result of 1 fn and injecting into the next No Monads tho! compose.(-> n { n + 1}, -> n { n * 2 }).(10)

# File lib/funcify/fn.rb, line 164
def compose
  ->(*fns) { fns.reduce { |f, g| lambda { |x| f.(g.(x)) } } }
end
ctx_value() click to toggle source
# File lib/funcify/fn.rb, line 279
def ctx_value
  ->(v) { v.context }
end
delimiter_detokeniser() click to toggle source
# File lib/funcify/fn.rb, line 326
def delimiter_detokeniser
  -> delimiter, f, enum { map.(f, enum).join(delimiter) }.curry
end
delimiter_tokeniser() click to toggle source

Takes a collection and generates a string delimited by the delimiter (such as “|”) Returns a curryed fn (ready for the collection) that takes 2 params: @param f, a function that extracts the properties from a map; e.g. F.map.(F.identity) @param enum, the map

# File lib/funcify/fn.rb, line 322
def delimiter_tokeniser
  -> delimiter, f, enum { f.(enum).join(delimiter) }.curry
end
detokeniser(delimiter) click to toggle source
# File lib/funcify/fn.rb, line 330
def detokeniser(delimiter)
  ->(str) { str.split(delimiter) }.curry
end
either() click to toggle source

The little Either Cond returns either the result of f_ok || f_fail by applying the value to test t. > either.(maybe_value_ok, identity, maybe_failure).(Success(1)) => Success(1)

# File lib/funcify/fn.rb, line 143
def either
  ->(test, f_ok, f_fail, value) { test.(value) ? f_ok.(value) : f_fail.(value) }.curry
end
empty?() click to toggle source
# File lib/funcify/fn.rb, line 291
def empty?
  -> xs { xs.empty? }
end
equality() click to toggle source

+ field; the property to extract from the record. Either a String/Symb or a Proc which takes the record + test_value; the value which has == applied to determine equality + i; the record under test e.g. equality.(:a).(“equal”).({a: “equal”}) e.g. equality.(test_fn).(“equal”).({a: “equal”})) ; where test_fn is -> x { x }

# File lib/funcify/fn.rb, line 195
def equality
  ->( field, test_value, i ) {
    if field.kind_of?(Proc)
      field.(i) == test_value
    else
      i[field] == test_value
    end
  }.curry
end
failure() click to toggle source
# File lib/funcify/fn.rb, line 251
def failure
  -> v { Failure(v) }
end
find() click to toggle source

finds the first element in a collecton where f.(e) is true

# File lib/funcify/fn.rb, line 47
def find
  ->(f, enum) { enum.find { |e| f.(e) } }.curry
end
first() click to toggle source
# File lib/funcify/fn.rb, line 97
def first
  -> xs { xs.first }
end
flatten() click to toggle source
# File lib/funcify/fn.rb, line 112
def flatten
  -> xs { xs.flatten }
end
fmap() click to toggle source
# File lib/funcify/fn.rb, line 16
def fmap
  ->(f, enum) { enum.flat_map {|e| f.(e) } }.curry
end
fmap_compose() click to toggle source

Monadic Compose, using flat_map The result of a fn must return an Either. fmap_compose.([->(v) { M.Success(v + 1) }, ->(v) { M.Success(v + 10) }]).(M.Success(0))

# File lib/funcify/fn.rb, line 171
def fmap_compose
  ->(fns, value) {
    fns.inject(value) {|result, fn| result.success? ? result.fmap(fn).value_or : result}
  }.curry
end
fmapr_compose() click to toggle source

reverse version of fmap_compose

# File lib/funcify/fn.rb, line 178
def fmapr_compose
  ->(*fns) {
    -> x { fns.reverse.inject(x) {|x, fn| x.success? ? x.fmap(fn).value_or : x} }
  }
end
group_by() click to toggle source
# File lib/funcify/fn.rb, line 28
def group_by
  -> f, xs { xs.group_by { |x| f.(x) } }.curry
end
hash_to_tokens() click to toggle source
# File lib/funcify/fn.rb, line 361
def hash_to_tokens
  compose.(join.(","), Map.map.(-> k, v { "#{k}:#{v}"}))
end
identity() click to toggle source
# File lib/funcify/fn.rb, line 132
def identity
  ->(i) { i }
end
include() click to toggle source

x can either be an array or a string

# File lib/funcify/fn.rb, line 206
def include
  -> x, v { x.include?(v) }.curry
end
inject() click to toggle source
# File lib/funcify/fn.rb, line 24
def inject
  -> acc, f, xs { xs.inject(acc) {|acc, x| f.(acc).(x) } }.curry
end
join() click to toggle source
# File lib/funcify/fn.rb, line 59
def join
  -> sep, i { i.join(sep) }.curry
end
last() click to toggle source
# File lib/funcify/fn.rb, line 93
def last
  -> xs { xs.last }
end
lat() click to toggle source

left at; takes the enum and applies the key/index to it.

# File lib/funcify/fn.rb, line 235
def lat
  ->(i, x) { i[x] }.curry
end
lift() click to toggle source

lifts the value, otherwise returns nil

# File lib/funcify/fn.rb, line 117
def lift
  ->(f, with, i) { f.(i) ? with.(i) : nil }.curry
end
lift_monad() click to toggle source
# File lib/funcify/fn.rb, line 128
def lift_monad
  -> value { maybe_value_ok?.(value) ? maybe_value.(value) : maybe_failure.(value) }
end
lift_value() click to toggle source

Takes a structure (like a Monad), an OK test fn, and a fn to extract when OK Returns the result of f, otherwise nil > lift_value.(maybe_value_ok, maybe_value),

# File lib/funcify/fn.rb, line 124
def lift_value
  ->(value_type, f) { Fn.lift.(value_type, f) }.curry
end
linclusion() click to toggle source
# File lib/funcify/fn.rb, line 216
def linclusion
  ->( field, value, i ) { i[field].include?(value) }.curry
end
map() click to toggle source

Common curried map higher order fn > map.(-> i { i.to_s } ).([1,2,3])

# File lib/funcify/fn.rb, line 12
def map
  ->(f, enum) { enum.map {|e| f.(e) } }.curry
end
match() click to toggle source

takes a regex and applies it to a value

# File lib/funcify/fn.rb, line 221
def match
  ->(r, i) { i.match(r) }.curry
end
max() click to toggle source
# File lib/funcify/fn.rb, line 67
def max
  ->(f, enum) { f.(enum).max }.curry
end
max_int() click to toggle source
# File lib/funcify/fn.rb, line 247
def max_int
  -> limit, i { i > limit ? limit : i }.curry
end
maybe_failure() click to toggle source
# File lib/funcify/fn.rb, line 271
def maybe_failure
  ->(v) { v.failure }
end
maybe_pipeline() click to toggle source

Provides a Maybe pipeline wrapped in a Lambda. This allows the pipeline functions to be applied first, and returns a function which allows the injection of the params to be applied into the beginning of the pipeline. e.g. pipeline = maybe_pipeline.([-> x { Success(x + 1) } ] ) pipeline.value_or.(Success(1)) => Success(2)

# File lib/funcify/fn.rb, line 341
def maybe_pipeline
  ->(pipeline) {
    Success(lambda do |value|
      pipeline.inject(value) do |result, fn|
        result.success? ? result.fmap(fn).value_or : result
      end
    end)
  }
end
maybe_value() click to toggle source
# File lib/funcify/fn.rb, line 267
def maybe_value
  ->(v) { v.value_or }
end
maybe_value_fail?() click to toggle source
# File lib/funcify/fn.rb, line 263
def maybe_value_fail?
  -> v { v.respond_to?(:failure?) && v.failure? }
end
maybe_value_ok?() click to toggle source
# File lib/funcify/fn.rb, line 259
def maybe_value_ok?
  ->(v) { v.respond_to?(:success?) && v.success? }
end
merge() click to toggle source
# File lib/funcify/fn.rb, line 32
def merge
  -> to, with { to.merge(with) }.curry
end
method() click to toggle source
# File lib/funcify/fn.rb, line 136
def method
  -> m, obj { obj.send(m) }.curry
end
method_caller() click to toggle source
# File lib/funcify/fn.rb, line 283
def method_caller
  -> obj, method, v { obj.send(method, v) }.curry
end
none?() click to toggle source
# File lib/funcify/fn.rb, line 79
def none?
  ->(f, enum) { enum.none? { |e| f.(e) } }.curry
end
nothing() click to toggle source
# File lib/funcify/fn.rb, line 299
def nothing
  -> x { nil }
end
partition() click to toggle source

> partition.(-> x { x == 1 }).([1,1,2,3,4])

# File lib/funcify/fn.rb, line 89
def partition
  -> f, enum { enum.partition { |e| f.(e) } }.curry
end
present?() click to toggle source
# File lib/funcify/fn.rb, line 295
def present?
  -> x { x.present? }
end
remove() click to toggle source

Curryed fn that removes elements from a collection where f.(e) is true

# File lib/funcify/fn.rb, line 37
def remove
  ->(f, enum) { enum.delete_if {|e| f.(e) } }.curry
end
remove_nil() click to toggle source
# File lib/funcify/fn.rb, line 303
def remove_nil
  Fn.remove.(->(i) { i.nil? } )
end
replace() click to toggle source
# File lib/funcify/fn.rb, line 55
def replace
  ->(r, with, s) { s.gsub(r,with)  }.curry
end
rest() click to toggle source
# File lib/funcify/fn.rb, line 101
def rest
  -> xs {
    _a, *b = xs
    b
  }
end
rinclude() click to toggle source

Right Include, where the value is applied partially waiting for the test prop

# File lib/funcify/fn.rb, line 211
def rinclude
  -> v, x { x.include?(v) }.curry
end
select() click to toggle source
# File lib/funcify/fn.rb, line 51
def select
  ->(f, enum) { enum.select { |e| f.(e) } }.curry
end
sequence() click to toggle source
# File lib/funcify/fn.rb, line 20
def sequence
  ->(fs, i) { fs.inject([]) { |r, f| r << f.(i) } }.curry
end
split() click to toggle source
# File lib/funcify/fn.rb, line 63
def split
  -> sep, i { i.split(sep) }.curry
end
status_value_ok?() click to toggle source
# File lib/funcify/fn.rb, line 275
def status_value_ok?
  ->(v) { v.status == :ok }
end
success() click to toggle source
# File lib/funcify/fn.rb, line 255
def success
  -> v { Success(v) }
end
take() click to toggle source
# File lib/funcify/fn.rb, line 225
def take
  ->(f, i) { f.(i) unless i.nil? }.curry
end
tests() click to toggle source

success_fn: a test fn to apply to the enum resulting from applying the tests; e.g. Fn.all? (and) or Fn.any? (or) test_fns : [test_fn]; each test is called with (value) value : the test context (can be anything understood by the tests) > tests.(all?, [-> x { x == 1}]).(1) => true

# File lib/funcify/fn.rb, line 151
def tests
  -> success_fn, test_fns, value {
    Fn.compose.(
      success_fn.(Fn.identity),                   # provide a results extractor fn to the success_fn
      Fn.map.(-> test_fn { test_fn.(value) } )    # call each test fn with the context
    ).(test_fns)
  }.curry
end
uniq() click to toggle source

> uniq.(identity).()

# File lib/funcify/fn.rb, line 84
def uniq
  -> f, enum { enum.uniq { |e| f.(e) } }.curry
end
when_nil?() click to toggle source
# File lib/funcify/fn.rb, line 108
def when_nil?
  ->(i) { i.nil? }
end
wrapper() click to toggle source

takes a function which is ready to be executed and wraps it in a function which will finally invoke it, by calling with empty arguments

# File lib/funcify/fn.rb, line 353
def wrapper
  -> fn { -> { fn } }
end