• Stars
    star
    153
  • Rank 243,368 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Haskell-style partial application and composition for Ruby methods

Funkify

Haskell-style partial application and composition for Ruby methods

Function composition when used in conjunction with partial application can yield exceptionally concise code, often more concise than the idiomatic Ruby. Check out the links below for further explanations of these features and examples of their use in Haskell:

Partial application in Haskell
Function composition in Haskell
Currying in Haskell

Also, watch this video to learn about some of the cool things you can do with function composition and partial application (the video is in Javascript but the ideas still apply to Ruby with Funkify)

Usage

Autocurrying

In order for a Ruby method to be amenable to partial application and composition it must first be autocurried:

class MyFunkyClass
  include Funkify

  # we make a specific method autocurried using the auto_curry method (Here with Ruby 2.0 decorator syntax)
  auto_curry def add(x, y)
    x + y
  end

  # alternatively, if we invoke auto_curry without a parameter
  # then all subsequent methods will be autocurried
  auto_curry

  def mult(x, y)
    x * y
  end

  def negate(x)
    -x
  end
end

Partial application and currying

When a method supports autocurrying it can still be invoked normally (if all parameters are provided) however if less than the required number are given a Proc is returned with the given parameters partially applied:

funky = MyFunkyClass.new

funky.add(1, 2) #=> This works normally and returns 3
add_1 = funky.add(1) #=> The `1` is partially applied and a `Proc` is returned
add_1.(2) #=> 3 (We invoke that `Proc` with the remaining argument)

Function composition

We compose methods using the * and | operators.

* composes right to left, this is the standard way to compose functions found in languages like Haskell:

(mult(5) * add(1)).(10) #=> 55

# We can further compose the above with another method:
(negate * mult(5) * add(1)).(10) #=> -55

| composes left to right, like a shell pipeline:

(mult(5) | add(1) | negate).(3) #=> -16

As a cute bonus, we can inject values from the left into a pipeline with the pass method together with the >= (pipeline operator) (see more):

pass(3) >= mult(5) | add(1) | negate #=> -16

Other examples:

Add 10 to every item in an Enumerable:

(1..5).map &funky.add(10) #=> [11, 12, 13, 14, 15]

Multiply by 10 and negate every item in an Enumerable:

(1..5).map &(funky.negate * funky.mult(10)) #=> [-10, -20, -30, -40, -50]

Installation

Add this line to your application's Gemfile:

gem 'funkify'

And then execute:

$ bundle

Or install it yourself as:

$ gem install funkify

Dedication

This library was inspired in part by stimulating conversations with epitron on Freenode.

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

More Repositories

1

binding_of_caller

Retrieve the binding of a method's caller in MRI 1.9.2+
Ruby
661
star
2

method_source

return the sourcecode for a method
Ruby
355
star
3

plymouth

Start Pry in the context of a failed test
Ruby
178
star
4

texplay

image manipulation tool for ruby and gosu
C
66
star
5

devil

ruby bindings for devil cross platform image loading library
Ruby
60
star
6

debug_inspector

A Ruby wrapper for the MRI 2.0 debug_inspector API
Ruby
58
star
7

free

force immediate garbage collection of an object
C
36
star
8

remix

Ruby modules re-mixed and remastered
Ruby
32
star
9

include_complete

Fixing the limitations in traditional Module#include
Ruby
19
star
10

pry-stack_explorer

MOVED TO https://github.com/pry/pry-stack_explorer
Ruby
18
star
11

prepend

prepends modules in front of a class; so method lookup starts with the module
C
16
star
12

selene

ruby spaceship game
Ruby
13
star
13

tremolo

Worms-like game for Ruby
Ruby
10
star
14

method_reload

fine grained code reloading
Ruby
10
star
15

state-ology

Clean and fast Object state transitions in Ruby using the Mixology C extension
Ruby
9
star
16

object2module

Convert Classes and Objects to Modules so they can be extended/included.
Ruby
9
star
17

pry-doc

Provide MRI Core documentation and source code for the Pry REPL
Ruby
9
star
18

mixology19

A version of the Mixology C extension that is 1.9.1 and 1.8.6 compatible
Ruby
8
star
19

pry-exception_explorer

MOVED TO https://github.com/pry/pry-exception_explorer
Ruby
7
star
20

gen_eval

a strange new breed of instance_eval
C
6
star
21

tweak

enhance classes with temporary functionality
Ruby
4
star
22

compat

Managing Ruby 1.9 and 1.8 C extension compatibility in one header file
C
3
star
23

custom_boolean

a hack to have if/else_if/else conditions with user-defined truthiness
Ruby
3
star
24

dup_eval

A souped up version of instance_eval in the vein of mix_eval
Ruby
3
star
25

pry-note

Ease refactoring and exploration by attaching notes to methods and classes in Pry"
Ruby
3
star
26

strange_evals

a hodgepodge of alternatives to instance_eval
Ruby
3
star
27

local_eval

instance_eval without changing self
Ruby
3
star
28

mult

a few utility functions written in C
C
2
star
29

basis

personal system setup for new *nix boxes
Shell
2
star
30

change_class2

change class of an object
Ruby
2
star
31

window-rotate-for-emacs

rotate the buffers for the active windows in emacs
2
star
32

pry_time

pry error console
Ruby
1
star
33

RailsExceptionExamples

Pry's @banisterfiend wanted a sample rails application to use for developmentโ€ฆ This is it with documentation.
Ruby
1
star
34

raise_shim

A shim for rb_raise
C
1
star
35

rumi

magic
C++
1
star
36

exercise

learning objc
Objective-C
1
star
37

baseline

organize project benchmarks into contexts
Ruby
1
star