Giter Site home page Giter Site logo

lambda_driver's Introduction

LambdaDriver

虚弦斥力場生成システム

LambdaDriver drives your code more functional.

# [:foo, :bar, :baz].map{|s| s.to_s }.map{|s| s.upcase }
# [:foo, :bar, :baz].map(&:to_s).map(&:upcase)

[:foo, :bar, :baz].map(&:to_s >> :upcase ) # => ["FOO",  "BAR",  "BAZ"]
# [:foo, :hoge, :bar, :fuga].select{|s| s.to_s.length > 3} # => [:hoge, :fuga]

[:foo, :hoge, :bar, :fuga].select(&:to_s >> :length >> 3._(:<)) # => [:hoge, :fuga]

Build status

Build Status

Installation

Add this line to your application's Gemfile:

gem 'lambda_driver'

And then execute:

$ bundle

Or install it yourself as:

$ gem install lambda_driver

Usage

Proc/lambda/Symbol/Method extensions

  • call
  • compose
  • with_args
  • flip
  • curry

alias :< to Proc#call

  f = lambda{|x| x.to_s }
  f < :foo # => "foo"

alias :+@ to Proc#to_proc

  +:to_s           # => #<Proc:0x007ff78aadaa78>
  +:to_s < :foo    # => "foo"

Proc#compose

Returns new lambda which composed self and given function. A composed proc called with args, executes `self.(g(*args)).

  f = lambda{|x| x.to_s * 2 }
  g = lambda{|y| y.length }

  h = f.compose g  # => #<Proc:0x007ff78aa2ab2>
  h.(:hoge)        # => "44" ( == f.call(g.call(:hoge)) )

This method is aliased as <<.

  f << g          # => f.compose(g)
  f << g < :hoge  # => "44" ( == f.call(g.call(:hoge)) )

Proc#lift

Lift this function to the given context-function. The lifted fucntion can compose other function with context-fucntion.

The given context-fuction used by compose_with_lifting to compose other fucntion.

The context-funciton should recieve 2 arguments.

  • first one is a function that reciver function of compose_with_lifting method.
  • second arg is a result of g(x)
    • g is a function passed to compose_with_lifting

If given arguments is Symbol, find context-function from default context-functions.

  • :identify
    • this context nothing to do
  • :maybe
    • computations which may not return a result
  • :list
    • computations which can return multiple possible results
  • :reader
    • computations which read from a shared environment
  • :writer
    • computations which write data in addition to computing values

see -> LambdaDriver::Context

Proc#compose_with_lifting

Compose self and given function on the context-function. The context-funciton is passed by lift method.

This method returns composed funciton like bellow.

 lambda{|args|  context(self, g(*args)) }

For example, set context-function that logging the result.

  hash = {:a => "foo"}
  f = lambda{|x| x.length}
  g = lambda{|y| hash[y]}

  ctx = lambda{|f,x|
    puts "g(x)    -> #{x}"
    y = f.call(x)
    puts "f(g(x)) -> #{y}"
    y
  }

  lifted = f.lift(ctx)
  h = lifted.compose_with_lifting g

  h.(:a)
  #=>  g(x)    -> foo # output by ctx
  #=>  f(g(x)) -> 3   # output by ctx
  #=> 3

if context-function does not given, default behaivior is compose function with checking g(x) is mzoro

if g(x) is mzero, it does not call self and return g(x), otherwise returns f(g(x)).

mzero means the object is nil or emtpy

  hash = {:a => "foo"}
  f = lambda{|y| y.length }
  g = lambda{|y| hash[y]}
  h = f.compose_with_lifting g
  h.(:a) # => 3
  h.(:b) # => nil (it does not called f)

This method is aliased as <=.

  f <= g # => f.compose_with_lifting(g)
Example : try-chains

An annoying try chain like arr.try(:first).try(:upcase).try(:to_sym) is rewritten by following

  arr = ["foo", "bar"]
  arr.try(&:first >= :upcase >= :to_sym) # => :Foo
  
  arr = [nil]
  arr.try(&:first >= :upcase >= :to_sym) # => nil

Proc#with_args

Returns partially applied function that has 2nd and more parameters are fixed by given *args.

  f = lambda{|x, y, z| [x, y, z]}

  h = f.with_args(:a, :b)   # => #<Proc:0x007ff78a9c5ca0>
  h.(:c)                    # => [:c, :a, :b] ( == f.call(:c, :a, :b) )

This method is aliased as *.

  f = lambda{|x, y| [x, y]}

  f * :foo          # => #<Proc:0x007ff78a987540> (== f.with_args(:foo) )
  f * :foo < :bar   # => [:bar,  :foo] ( == f.with_args(:foo).call(:bar) )

Proc#flip

Returns function whose parameter order swaped 1st for 2nd. A result of filped fuction is curried by Proc#curry.

  f = lambda{|x, y, z| [x, y, z]}

  h = f.flip                    # => #<Proc:0x007ff78a942fa>
  h.call(:a).call(:b).call(:c)  # => [:b, :a, :c] (== f.curry.call(:b).call(:a).call(:b))
  h < :a < :b < :c              # => [:b, :a, :c] (== f.curry.call(:b).call(:a).call(:b))

If arguments is var-args, pass explicitly arity to curring.

  p = Proc.new{|*args| args.inspect }

  p.arity                           # => -1
  p.flip(3).call(:a).(:b).(:c)      # => "[:b, :a, :c]"
  p.flip(4).call(:a).(:b).(:c).(:d) # => "[:b, :a, :c, :d]"

If arity is 0 or 1, flip returns itself.

This method is aliased as ~@.

  ~f # =>             #<Proc:0x007ff78a8e22c> (== f.filp)
  ~f < :a < :b < :c   # => [:b, :a, :c] (== f.filp.call(:b).call(:a).call(:b))

Symbol extensions

  • to_method
  • to_method_with_args

Symbol#to_method

Symbol#to_method generates a function that extract Method object from given argument. This method is aliased as -@.

  (-:index).call("foobarbaz")             # => #<Method: String#index>
  (-:index).call("foobarbaz").call("bar") # => 3 (== "foobarbaz".index("bar") )

  -:index < "foobarbaz"         # => #<Method: String#index>
  -:index < "foobarbaz" < "bar" # => 3 (== "foobarbaz".index("bar") )

Symbol#to_method_with_args

Symbol#to_method_with_args generates a function that extract Method object from given object, and returns function is partially applied parameters by passed arguments. It is same as Symbol#to_method with Proc#with_args.

This method is aliased as &.

  :index.to_method_with_args("bar")                   #  => #<Proc:0x007ffef4886ff8
  :index.to_method_with_args("bar").call("foobarbaz") #  => 3 (== "foobarbaz".index("bar") )

  :index & "bar"               #  => #<Proc:0x007ffef4886ff8
  :index & "bar" < "foobarbaz" #  => 3 (== "foobarbaz".index("bar") )

Class extensions

  • alias instance_method, :/
  String / :index # => #<UnboundMethod: String#index>

UnboundMethod extensions

  • alias bind, :<
  String / :index                    # => #<UnboundMethod: String#index>
  String / :index < "foobarbaz"      # => #<Method: String#index>
  String / :index < "foobarbaz" < 3  # => 3 (== "foobarbaz".index("bar") )

Combinators

  • ski combinator

Object extensions

  • obj.revapply(|>)
  • obj._
  • obj.disjunction(f)

Object#revapply

Object#revapply is applies self to given proc/block.

  f = lambda{|x| x * 2 }

  "foo".revapply(f) #  => "fooffoo" (== f.call("foo") )

Object#_

Object#_ is shortcut to quickly extract Method object.

"foobarbaz"._.index         # => #<Method: String#index>
"foobarbaz"._.index < "bar" # => 3 (== "foobarbaz".index("bar") )

2._(:>=)                    # => #<Method: Fixnum#>=>
[1, 2, 3].select(&2._(:>=)) # => [1, 2]( = [1, 2].select{|n| 2 >= n})

Object#disjunction

Object#disjunction select self or result of applied self to given function. if f(self) is nil, returns self, otherwise return f(self).

  f = lambda{|x| x % 2 == 0 ? nil : x * 2}

  2.disjunction(f) # => 2 (disjunction returns reciever object)
  3.disjunction(f) # => 6 (disjunction returns f(3) )

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

lambda_driver's People

Contributors

yuroyoro avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.