Giter VIP home page Giter VIP logo

sloth's Introduction

Sloth

Experiments with laziness, lists and functional Haskell-style programming in Typescript.

Examples

Fibonacci

A Haskell program to efficiently calculate the Fibonacci numbers is as follows:

fibs :: [Integer]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)

This astounding one liner takes advantage of the laziness of lists, and now it is possible to horribly recreate this in Typescript, causing much pain to many programmers of many lifestyles and paradigms:

let fibs: List<number> = List.cons(1, new List(() =>
    Maybe.just([1, zipWith((x, y) => x + y, fibs, fibs.tail())])
));

Monadic List Comprehensions

In Haskell, we may like to do a list comprehension to retrieve all Pythagorean triples:

pythag = [(x, y, z) | z <- [1..], y <- [1..z], x <- [y..z], x * x + y * y == z * z]

Alternatively, since lists form a monad, we may write this in the following do-style notation:

pythag :: [(Int, Int, Int)]
pythag = do
  z <- [1..]
  y <- [1..z]
  x <- [y..z]
  guard $ x * x + y * y == z * z
  pure (x, y, z)

Once more, we shall kill several puppies and cause at least one person to punch their monitor:

const pythag: List<[number, number, number]> = List
  .range(1, Infinity)
  .bind(z => List.range(1, z)
  .bind(y => List.range(y, z)
  .bind(x => List
  .guard(x * x + y * y == z * z)
  .bind(_ => List
  .pure([x, y, z])
))));

sloth's People

Contributors

avarsh avatar

Watchers

James Cloos 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.