Git Product home page Git Product logo

mll's Introduction

MLL (Wolfram Mathematica Language Library)

Join the chat at https://gitter.im/Nakilon/mll
Gem Version
Build Status

What

This gem isn't supposed to mimic all data types, exact syntax of Wolfram Mathematica or make Ruby able to make the same visualisations.

The main goal is to make Ruby more powerful by including the most used functions, that Ruby lacks, such as Table[], FoldList[], etc. Visualisations are possible later by using additional gems.

Why

  1. ::map can map Arrays at specified depth!
  2. One of the most useful things is automatic zipping vectors (Arrays) when you apply scalar functions to them. https://reference.wolfram.com/language/ref/Listable.html
  3. unlike Ruby's Range class, ::range can handle negative step and even have float starting value
  4. unlike Ruby's Array.new, ::table can create multidimensional arrays with a single call, not nested
  5. ::fold_list was wanted here in Ruby while being already implemented as FoldList[] in Mathematica and scanl in Haskell
  6. ::nest (n times) and ::nest_list for repetitive applying the same function -- ::nest_while and ::nest_while_list are going to be implemented later
  7. ::tally -- shortcut to a probably the most common usage of #group_by -- calculating total occurences.
  8. TODO write smth about ::riffle, ::subdivide, ::grid

How

MLL::range[ 2, 3 ] # => 2..3
MLL::range[[2, 3]] # => [1..2, 1..3]
MLL::range[ 1..3 ] # => [1..1, 1..2, 1..3]

MLL::table[ MLL::times, 9, 9 ]
                   # => [[1,  2,  3,  4,  5,  6,  7,  8,  9],
                         [2,  4,  6,  8, 10, 12, 14, 16, 18],
                         [3,  6,  9, 12, 15, 18, 21, 24, 27],
                         [4,  8, 12, 16, 20, 24, 28, 32, 36],
                         [5, 10, 15, 20, 25, 30, 35, 40, 45],
                         [6, 12, 18, 24, 30, 36, 42, 48, 54],
                         [7, 14, 21, 28, 35, 42, 49, 56, 63],
                         [8, 16, 24, 32, 40, 48, 56, 64, 72],
                         [9, 18, 27, 36, 45, 54, 63, 72, 81]]

# similar to Ruby's #product with #map...
t = MLL::table[ ->(i,j){ i+j }, [[1, 0, 1]], [[0, 2, 0]] ]
                   # => [[1, 3, 1],
                         [0, 2, 0],
                         [1, 3, 1]]
# ... but our #map...
t = MLL::map[ ->(i){ [i] }, t, [2] ]
                   # => [[ [1], [3], [1] ],
                         [ [0], [2], [0] ],
                         [ [1], [3], [1] ]]
# ... can go deeper
MLL::map[ ->(i){ -i }, t, [3] ]
                   # => [[ [-1], [-3], [-1] ],
                         [ [ 0], [-2], [ 0] ],
                         [ [-1], [-3], [-1] ]]

# ::times     means  *
# ::divide    means  /
# ::subtract  means  -
# ::plus      means  +

# here is another `Listable' magic, allowing to zip arrays
#   even of different dimensions with basic operations
MLL::times[ [[1,2],[3,4]], [5,6] ]
                   # => [[5,10], [18,24]]

# listing factorials # http://stackoverflow.com/a/3590520/322020
MLL::fold_list[ MLL::times, MLL::range[8] ]
                   # => [1, 2, 6, 24, 120, 720, 5040, 40320]

MLL::fold_list[ ->(a,b){ 10*a + b }, 0, [4,5,1,6,7,8] ]
                   # => [0, 4, 45, 451, 4516, 45167, 451678]

# http://en.wikipedia.org/wiki/Collatz_conjecture
MLL::nest_list[ ->(i){ i.even? ? i/2 : (i*3+1)/2 }, 20, 10 ]
                   # => [20, 10, 5, 8, 4, 2, 1, 2, 1, 2, 1]

# counting characters and nice printing the resulting table
MLL::grid[ MLL::tally[ "the quick brown fox jumps over the lazy dog".chars ].
             sort_by(&:last).reverse.take(10),
           frame: :all, spacings: [2, 0] ]
                   # => ┏━━━┳━━━┓
                        ┃   ┃ 8 ┃
                        ┃ o ┃ 4 ┃
                        ┃ e ┃ 3 ┃
                        ┃ u ┃ 2 ┃
                        ┃ h ┃ 2 ┃
                        ┃ r ┃ 2 ┃
                        ┃ t ┃ 2 ┃
                        ┃ n ┃ 1 ┃
                        ┃ p ┃ 1 ┃
                        ┃ m ┃ 1 ┃
                        ┗━━━┻━━━┛
# current implementation of #grid sucks and needs your help ,.)

MLL::riffle[ "4345252523535".chars, ",", [-4,1,-4] ]
                   # => "4,345,252,523,535"

MLL::subdivide[ 5, 10, 4 ]
                   # => [5.0, 6.25, 7.5, 8.75, 10.0]

MLL::most[ [1, 2, 3, 4] ]  # => [1, 2, 3]
# now it's possible to extend core classes with some of those methods
require "mll/core_ext"
[1, 2, 3, 4].most          # => [1, 2, 3]

Note that to see some of above examples working in the same way you need .to_a, .map(&:to_a) or even .to_a.map(&:to_a) since lazyness is intensively used.

Installation

$ gem install mll

Testing with RSpec before contributing

rspec

or

rake spec

or

rake    # to implicitly run 'rake todo'

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.