Coder Social home page Coder Social logo

el-arrows's Introduction

Arrows

A set of Threading macro’s blatantly rewritten from cl-arrows

Roadmap

Arrows to implement

  • [ ] nil short circuiting arrows, these will return nil if any of the forms return nil.

Reference

Macros

Macro nameDescriptionNickname
arr->argument first. Identical to thread-firsttip
arr->>argument last. Identical to thread-lastspear
arr-<>argument first with placeholdersdiamond wand
arr-<>>argument last with placeholdersdiamond spear

each of them has a fn variant which allows for “curried” like function syntax they take the name of arr-fn-NAME so for example arr-fn-<>>

Functions

Function nameDescriptionarguments
arr-inspecta way to quickly print out the value in a pipeline without disrupting itvalue &optional &key print-fn label

Examples

Simple threading

here we are just pushing a number through a contrived pipeline

(arr-> 8 (1+) (number-to-string) (string-pad 5) (string-pad 9 nil t))
"     9      "

Here we are using ->> to pass this list in as the last argument of these maps and filters.

(arr->> '(1 2 3 4 5)
        (seq-map #'1+)
        (seq-filter #'cl-evenp))
246

placeholder example

If we go back to that last example we see a limitation, we can only thread last, we could not use seq-reduce here as it requires the sequence to be the 2nd argument out of 3 (placing it in the middle). This is where diamond variants come into play.

(arr-<>> '(1 2 3 4 5)
         (seq-map #'1+)
         (seq-filter #'cl-evenp)
         (seq-reduce #'+ <> 0))
12

Here <> is a placeholder meaning our value is passed into that spot, this makes it very easy to compose functions that might not have uniform positioning of the passed argument (like with seq).

fn example

We also provide composition functions which are useful when you want to have a function that represents a set of transformations. this also allows your code to look point free. All arrow macro’s have a fn variant

(require 'arr)

(seq-map (arr-fn-> (1+) (number-to-string)) '(1 2 3))
234

Lambda short hands

as a side effect this allows for short functions without the need for the full lambda syntax. This is not as nice nor as flexible as something like dollar.el but still is worth mentioning IMHO

(seq-map (arr-fn-<> (* <> <>)) '(2 3 4))
4916

el-arrows's People

Contributors

jeetelongname avatar

Watchers

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