Giter Site home page Giter Site logo

functional-ruby's Introduction

Functional Ruby

Installing

gem install fn-ruby

require 'functional-ruby'

There are some utils for working in ruby in functional style.

Currying, first parameters (apply_head, >>, %)

pow = -> x, y { x ** y }
(pow % 2)[3] #=> 8

Currying, last parameters (apply_tail, <<)

pow = -> x, y { x ** y }
(pow << 2)[3] #=> 9

Composing (compose, *)

f = -> x { x * x }
g = -> x { x + 1 }
(f * g)[3] #=> 16

Mapping (map, |)

f = -> x { x * x }
d = [1, 2, 3]
f | d #=> [1, 4, 9]

Reducing (reduce, <=)

f = -> a, e { a * e }
d = [1, 2, 3]
f <= d #=> 6

Memoization (memoize, +@)

You can make your function memoized just by typing + before it.

fact = +-> n { n < 2 ? 1 : n * fact[n - 1] }
t = Time.now; fact[1000]; Time.now - t #=> 0.018036605
t = Time.now; fact[1000]; Time.now - t #=> 2.6761e-05

Arithmetic

You can use usual arithmetic operators.

+
f = -> x { x * x }
g = -> x { x + 1 }
(f + g)[3] #=> 13
-
f = -> x { x * x }
g = -> x { x + 1 }
(f - g)[3] #=> 5
/
f = -> x { x * x }
g = -> x { x + 1 }
(f / g)[3] #=> 2
mult

The * method is used for composing so we provide symbolic name for this.

f = -> x { x * x }
g = -> x { x + 1 }
f.mult(g)[5] #=> 36

Working with methods

The Symbol#@+ method is overloaded.

def sqr(x)
  x * x
end

def inc(x)
  x + 1
end

(+:sqr * +:inc)[3] #=> 16

Author (Speransky Danil): Personal Page | LinkedIn | GitHub | StackOverflow

functional-ruby's People

Stargazers

 avatar

Watchers

 avatar  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.