All Projects → delonnewman → invokable

delonnewman / invokable

Licence: MIT License
Objects are functions! Treat any Object or Class as a Proc (like Enumerable but for Procs).

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to invokable

Collection
A PHP library for representing and manipulating collections.
Stars: ✭ 488 (+1120%)
Mutual labels:  set, array, hash
activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (-47.5%)
Mutual labels:  ruby-gem, set
ftor
ftor enables ML-like type-directed, functional programming with Javascript including reasonable debugging.
Stars: ✭ 44 (+10%)
Mutual labels:  composition, currying
total-serialism
Toolbox full of Algorithmic Composition methods
Stars: ✭ 74 (+85%)
Mutual labels:  array, composition
php-helpers
An extensive set of PHP helper functions and classes.
Stars: ✭ 27 (-32.5%)
Mutual labels:  functions, array
common
Metarhia Common Library
Stars: ✭ 55 (+37.5%)
Mutual labels:  array, curry
chimera
Lazy infinite compact streams with cache-friendly O(1) indexing and applications for memoization
Stars: ✭ 47 (+17.5%)
Mutual labels:  memoize, memoization
Pluck to hash
Extend ActiveRecord pluck to return array of hashes
Stars: ✭ 275 (+587.5%)
Mutual labels:  ruby-gem, hash
kashe
A memoization library based on weakmaps. 🤯 Sometimes cache is kashe
Stars: ✭ 60 (+50%)
Mutual labels:  memoize, memoization
hu
Small, generic functional helper library for node.js and browsers
Stars: ✭ 18 (-55%)
Mutual labels:  memoize, curry
VBA-Arrays
😎 Array functions that are similar JavaScript functions. Example: Push, Pop, Shift, Unshift, Sort, length, toString.
Stars: ✭ 48 (+20%)
Mutual labels:  functions, array
J-Curry
A Java library that enables applying Functional Programming concepts like currying and partial application for functions, also it supports types like Either, Try, etc... using RxJava 2 interfaces, compatible with Java 7 and above
Stars: ✭ 17 (-57.5%)
Mutual labels:  curry, currying
kudojs
A utility library to write code in functional programming style in Javascript
Stars: ✭ 22 (-45%)
Mutual labels:  composition, curry
PartialFunctions.jl
A small package to simplify partial function application
Stars: ✭ 34 (-15%)
Mutual labels:  curry, currying
memo wise
The wise choice for Ruby memoization
Stars: ✭ 486 (+1115%)
Mutual labels:  ruby-gem, memoization
vector
A PHP functional programming library.
Stars: ✭ 19 (-52.5%)
Mutual labels:  memoization, currying
array-sort-by
Powerful mechanism to sort arrays or array of objects by one or more properties. You can also specify a custom comparer function.
Stars: ✭ 37 (-7.5%)
Mutual labels:  array, objects
Containers
This library provides various containers. Each container has utility functions to manipulate the data it holds. This is an abstraction as to not have to manually manage and reallocate memory.
Stars: ✭ 125 (+212.5%)
Mutual labels:  set, array
Core
Elm's core libraries
Stars: ✭ 2,634 (+6485%)
Mutual labels:  set, array
expand-hash
Recursively expands property keys with dot-notation into objects.
Stars: ✭ 25 (-37.5%)
Mutual labels:  hash, objects

Ruby Gem Version codebeat badge

Invokable

Objects are functions! Treat any Object or Class as a Proc (like Enumerable but for Procs)

Synopsis

Objects that enclose state, can be treated as automatically curried functions.

require 'invokable'

class TwitterPoster
  include Invokable

  def initialize(model)
    @model = model
  end

  def call(user)
    # do the dirt
    ...
    TwitterStatus.new(user, data)
  end
end

TwitterPoster.call(Model.find(1)) # => #<TwitterPoster ...>
TwitterPoster.call(Model.find(1), current_user) # => #<TwitterStatus ...>

# both the class and it's instances can be used anywhere Procs are.

Model.where(created_at: Date.today).map(&TwitterPoster) # => [#<TwitterPoster ...>, ...]

Use memoize, << and >> for composition and other methods on Proc on your command objects

# app/queries/filter_records.rb
class FilterRecords
  include Invokable

  def initialize(params)
    @params = params
  end

  def call(records)
    # do filtering return records
  end
end

# app/queries/sort_records.rb
class SortRecords
  include Invokable

  def initialize(params)
    @params = params
  end

  def call(records)
    # do sorting return records
  end
end

sort_and_filter = SortRecords.call(params) << FilterRecords.call(params)
sort_and_filter.call(records) # => sorted and filtered records

Helper methods that can be used with any object that responds to call or to_proc

Invokable.juxtapose(:sum, -> (xs) { xs.reduce(:*) }, :min, :max).([3, 4, 6]) # => [13, 72, 3, 6]

Invokable.knit(:upcase, :downcase).(['FoO', 'BaR']) # => ["FOO", "bar"]

They are also mixed into any class that includes the module

class Transformer
  include Invokable

  def call(array)
    array.map(&juxtapose(identity, compose(:to_s, :upcase))).to_h
  end
end

Transformer.call([:a, :b, :c, :d]) # => {:a => "A", :b => "B", :c => "C", :d => "D"} 

Hashes can be treated as functions of their keys

require 'invokable'
require 'invokable/hash'

number_names = { 1 => "One", 2 => "Two", 3 => "Three" }
[1, 2, 3, 4].map(&number_names) # => ["One", "Two", "Three", nil]

Arrays can be treated as functions of their indexes

require 'invokable'
require 'invokable/array'

alpha = ('a'..'z').to_a
[1, 2, 3, 4].map(&alpha) # => ["b", "c", "d", "e"]

Sets can be treated as predicates

require 'invokable'
require 'invokable/set'

favorite_numbers = Set[3, Math::PI]
[1, 2, 3, 4].select(&favorite_numbers) # => [3]

Use as much or a little as you need

require 'invokable'         # loads Invokable module
require 'invokable/helpers' # loads Invokable::Helpers module
require 'invokable/hash'    # loads hash patch
require 'invokable/array'   # loads array patch
require 'invokable/set'     # loads set patch
require 'invokable/data'    # loads hash, set and array patches

Why?

A function is a mapping of one value to another with the additional constraint that for the one input value you will always get the same output value. So, conceptually, Ruby Hashes, Arrays, and Sets are all functions. Also, there are many one method objects out there (e.g. Service Objects) that are essentially functions. Why not treat them as such?

Installation

Add this line to your application's Gemfile:

gem 'invokable'

And then execute:

> bundle

Or install it yourself as:

> gem install invokable

API Documentation

https://www.rubydoc.info/gems/invokable

See Also

Support

Tested using MRI 2.4, 2.5, 2.6, 2.7, 3.0 and JRuby

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].