Skip to content

🐏 πŸ€” - A library to help suggest functions based on input and desired output.

License

Notifications You must be signed in to change notification settings

domtronn/ramda-suggest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

47 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Ramda Suggest

🐏 πŸ€”

Usage | Non-Primitive Inputs | Functions as Outputs

This is a library inspired by suggest.el and the constant (valid) feedback I get from people new to Ramda...

"How do you know what the Ramda function is called?!" - πŸ€”

"I know what I want to do, but how do I do it?!" - 😫

"Why do the arguments for compose go right to left...?" - πŸ™ƒ

So this tool will (hopefully) suggest a function in Ramda, you can use to produce your desired output! For example,

// Given:   [1, 2, 3, 4, 5]
// Desired: 15

R.sum([1, 2, 3, 4, 5]) = 15

Usage

Install this package in the typical way;

npm install -g ramda-suggest

ramda-suggest true

# T  [type:Function] : R.T() β†’ true
# 
#  A function that always returns `true`. Any passed in parameters are ignored.
# 
#  param 1: {*} 
#  returns: {Boolean}
ramda-suggest 42 41

# dec  [type:Math] : R.dec(42) β†’ 41
# 
#  Decrements its argument.
# 
#  param 1: {Number} n
#  returns: {Number} n - 1

Non-Primitive Inputs 🐡

Be Warned! ⚠️

The easiest way allow inputs of things other that just primitive JavaScript data types (i.e. Arrays, Objects, Functions) was to use eval.

This will cast Strings that look like Arrays into Arrays! However, bear in mind that eval will just evaluate whatever you pass in in the following formats;

Strings
ramda-suggest foo bar foobar

# concat  [type:List] : R.concat("foo", "bar") β†’ foobar
# 
#  Returns the result of concatenating the given lists or strings.
#  Note: `R.concat` expects both arguments to be of the same type,
#  unlike the native `Array.prototype.concat` method. It will throw
#  an error if you `concat` an Array with a non-Array value.
#  Dispatches to the `concat` method of the first argument, if present.
# 
#  param 1: {Array|String} firstList The first list, param 2: {Array|String} secondList...
#  returns: {Array|String} A list consisting of the elements of `firstList` followed by...
Objects
ramda-suggest a '{a:1,b:2,c:3}' '{b:2,c3:}'

# dissoc [category:Object]
# 
#     R: R.dissoc("a", {"a":1,"b":2,"c":3}) β†’ {"b":2,"c":3}
#     Ξ»: String β†’ {k: v} β†’ {k: v}
# 
#  Returns a new object that does not contain a prop property.
# 
#  param 1: {String} prop The name of the property to dissociate
#  param 2: {Object} obj The object to clone
#  returns: {Object} A new object equivalent to the original but without the specified property

Note that Objects must be passed in as string - i.e. wrapped in quotes - This is a limitation of how Node parses command line args.

Arrays
ramda-suggest [1,2,3,4,5] 15
ramda-suggest '[1, 2, 3, 4, 5]' 15

# sum  [type:Math] : R.sum([1,2,3,4,5]) β†’ 15
# 
#  Adds together all the elements of a list.
# 
#  param 1: {Array} list An array of numbers
#  returns: {Number} The sum of all the numbers in the list.
Functions

Functions must be placed inside strings

ramda-suggest '(a, b) => a + b' 0 [1,2,3,4] 10

# reduce  [type:List] : R.reduce((a, b) => a + b, 0, [1,2,3,4]) β†’ 10
# 
#  /*
#  ...docs...
#  */
# 
#  param 1: {Function} fn The iterator function. Receives two values, the accumulator and the, param 2: {*} acc The accumulator value., param 3: {Array} list The list to iterate over.
#  returns: {*} The final, accumulated value.

Function Outputs πŸ™Š

A lot of the time, Ramda will return a function rather than actual output - You can also test these in the same way you would pass in functions as arguments! e.g.

ramda-suggest '(a) => a + 5' '() => 10' '() => 15'

# compose [category:Function]
# 
#     R: R.compose((a) => a + 5, () => 10) β†’ () => 15
#     Ξ»: ((y β†’ z), (x β†’ y), ..., (o β†’ p), ((a, b, ..., n) β†’ o)) β†’ ((a, b, ..., n) β†’ z)
# 
#  Performs right-to-left function composition. The rightmost function may have
#  any arity; the remaining functions must be unary.
# 
#  param 1: {...Function} ...functions The functions to compose
#  returns: {Function}
Complex output functions

For output functions which take arguments, you should pass them in using the following format;

("value_1", "value_2") => "return_string"

This will define an output function which when called with the 2 arguments, "value_1" & "value_2" (both strings) expects the return to be "return_string".

ramda-suggest '(a) => a + 2' '(a) => a * 2' '(5) => 12'

# compose [category:Function]
# 
#     R: R.compose((a) => a + 2, (a) => a * 2) β†’ (a: 5) => 12
#     Ξ»: ((y β†’ z), (x β†’ y), ..., (o β†’ p), ((a, b, ..., n) β†’ o)) β†’ ((a, b, ..., n) β†’ z)
# 
#  Performs right-to-left function composition. The rightmost function may have
#  any arity; the remaining functions must be unary.
# 
#  param 1: {...Function} ...functions The functions to compose
#  returns: {Function}

In the above example, you have two functions which take arguments, and when composed together basiaclly perform (a) => (a * 2) + 2, in this case, we would expect that when we call the returned function with a value of 5, we should get 12 out.

This would also work with the following examples

  • ramda-suggest '(a) => a + 2' '(a) => a * 2' '(10) => 22'
  • ramda-suggest '(a) => a + 2' '(a) => a * 2' '(100) => 202'
  • ramda-suggest '(a) => a + 2' '(a) => a * 2' '(2) => 6'
Complexer outputer functionser

You can also use other primtive types as return values from your output functions, for example

ramda-suggest '(a) => [a, 2]' '(a) => a * 2' '(100) => [200, 2]'
 
# compose [category:Function]
# 
#     R: R.compose((a) => [a, 2], (a) => a * 2) β†’ (a: 100) => [200,2]
#     Ξ»: ((y β†’ z), (x β†’ y), ..., (o β†’ p), ((a, b, ..., n) β†’ o)) β†’ ((a, b, ..., n) β†’ z)
# 
#  Performs right-to-left function composition. The rightmost function may have
#  any arity; the remaining functions must be unary.
# 
#  param 1: {...Function} ...functions The functions to compose
#  returns: {Function}

β–² back to top

About

🐏 πŸ€” - A library to help suggest functions based on input and desired output.

Resources

License

Stars

Watchers

Forks

Packages

No packages published