Skip to content

obfusk/obfusk.rb

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

[]: {{{1

File        : README.md
Maintainer  : Felix C. Stegerman <flx@obfusk.net>
Date        : 2014-11-23

Copyright   : Copyright (C) 2014  Felix C. Stegerman
Version     : v0.1.3

[]: }}}1

Gem Version Build Status

Description

obfusk.rb - functional programming library for ruby

Examples

[]: {{{1

require 'obfusk/adt'

class Foo
  include Obfusk::ADT
  constructor :Bar
  constructor :Baz, :value
end

x = Foo.Bar
y = Foo.Baz 99

y.value # => 99

x.match Bar: ->(_) { "it's a bar!" },
        Baz: ->(z) { "it's a baz with value: #{z.value}" }
# => "it's a bar!"

[]: }}}1

[]: {{{1

require 'obfusk/atom'

x = Obfusk.atom 42
x._ # => 42
10.times { x.swap! { |v| v + 1 } }
x._ # => 53

[]: }}}1

[]: {{{1

require 'obfusk/data'

Obfusk.merge([1,2,3], 1 => 99)
# => [1,99,3]

x = { x: { y: 0 }, z: [1,2,3] }

Obfusk.get_in x, :x, :y
# => 0

Obfusk.modify_in(x, :x, :y) { |v| v + 1 }
# => { x: { y: 1 }, z: [1,2,3] }

Obfusk.set_in(x, :z, 1, 99)
# => { x: { y: 0 }, z: [1,99,3] }

[]: }}}1

[]: {{{1

require 'obfusk/lazy'

x = Obfusk.lazy { some_expensive_computation_that_returns_42 }
x._ # => 42 (expensive computation not run until now)
x._ # => 42 (cached)

y = Obfusk.lazy { Foo.new 42 }
z = y.chain(:value)
z._ # => 42 (.new and .value not run until now)

Obfusk.eager(lazy_or_not) # => value

[]: }}}1

[]: {{{1

require 'obfusk/list'

fibs = Obfusk.List(0,1) { fibs.zipWith(fibs.tail, &:+) }

fibs == Obfusk.Nil
# => false

fibs.take(10).to_a
# => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

fibs.map { |x| x*x } .take(10).to_a
# => [0, 1, 1, 4, 9, 25, 64, 169, 441, 1156]

[]: }}}1

[]: {{{1

require 'obfusk/monad'

class Foo
  include Obfusk::Monad
  def self.mreturn(x)
    # ...
  end
  def self.bind_pass(m, &b)
    # ...
  end
end

f = -> x { '...' }
g = -> y { '...' }

Foo.new('...').pipeline f, g

[]: }}}1

[]: {{{1

require 'obfusk/monads'
ms = Obfusk::Monads

x = ms.Nothing
y = ms.Just 42

x.bind(y)
# => ms.Nothing

ms.Left "oops"
ms.Right 37

[]: }}}1

...

Specs & Docs

$ rake spec
$ rake coverage
$ rake docs

TODO

  • more lists operations
  • ...

License

LGPLv3+ [1].

References

[1] GNU Lesser General Public License, version 3 --- http://www.gnu.org/licenses/lgpl-3.0.html

[]: ! ( vim: set tw=70 sw=2 sts=2 et fdm=marker : )

About

obfusk.rb - functional programming library for ruby

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages