1.4.3 • Published 2 years ago

rfuncs v1.4.3

Weekly downloads
1
License
WTFPL
Repository
github
Last release
2 years ago

RFUNCS

A collection of amazing functions:

all

all(array)

  • Returns true if all values in the array are true
all()
// => true
all(true)
// => true
all(false)
// => false
all([true, true])
// => true
all([true, false])
// => false

all(array, func)

  • Returns true if all values in the array are true when applied to func
all(4, i => i <= 4)
// => true
all(4, i => i > 4)
// => false
all([2, 4], i => i <= 2)
// => false
all([2, 4], i => i <= 4)
// => true

any

any(array)

  • Returns true if at least one in the values in the array is true
any()
// => false
any(true)
// => true
any(false)
// => false
any([false, false])
// => false
any([true, false])
// => true

any(array, func)

  • Returns true if at least one in the values in the array is true when applied to func
any(4, i => i <= 4)
// => true
any(4, i => i > 4)
// => false
any([2, 4], i => i <= 2)
// => true
any([2, 4], i => i <= 4)
// => true
any([2, 4], i => i <= 1)
// => false

apply

apply(f, val₁, val₂, val₃, …)

  • returns f(val₁, val₂, val₃, …)
apply(a => a + 2, 1)
// => 3
apply(a => a + 2, [1])
// => 3
apply((a, b) => a + b, [1, 2])
// => 3
apply((a, b, c) => a + b + c, [1, 2, 3])
// => 6

composition

composition(f₁, f₂, f₃, …)

  • returns a composition of the functions f₁, f₂, f₃, …
composition()(4)
// => 4
composition(a => a + 2)(4)
// => 6
composition(
    a => a + 2,
    a => a * 3
)(4)
// => 18
composition(
    a => a + 2,
    a => a * 3,
    a => a / 2
)(4)
// => 9

contains

contains(data, value)

  • returns true if value is contained in data
contains([1, 2, 3], 2)
// => true
contains([1, 2, 3], 8)
// => false
contains({ a: 1, b: 2 }, "a")
// => true
contains({ a: 1, b: 2 }, "c")
// => false
contains(1, 1)
// => true

delete_duplicates

delete_duplicates(iterable)

  • deletes all duplicates from list.
delete_duplicates([1, 1, 3])
// => [1,3]
delete_duplicates([1, 7, 8, 4, 3, 4, 1, 9, 9, 2])
// => [1,7,8,4,3,9,2]
delete_duplicates([])
// => []
delete_duplicates([{ a: 2, b: 3 }, { a: 2 }], "a")
// => [{"a":2}]
delete_duplicates([{ a: 2, b: 3 }, { a: 2 }], o => o.a)
// => [{"a":2}]

filter

filter(data, crit)

  • picks out all elements xₑ of data for which crit(datae, e) is True
filter(v => v < 3, [1, 2, 3])
// => [1,2]
filter(v => v < 3, 1)
// => [1]
filter(v => v < 3, 5)
// => []
filter(v => v < 3, { a: 2, b: 5 })
// => {"a":2}
filter((v, index) => v == index, [0, 1, 2, 4])
// => [0,1,2]
filter((v, index) => v == index, 0)
// => [0]
filter((v, index) => v == index, 1)
// => []
filter((v, index) => v == index, { a: "a", b: "c" })
// => {"a":"a"}

first

first(1)
// => 1
first([1, 2, 3])
// => 1
first({ a: 1, b: 2 })
// => 1

getter

getter()({ a: 2 })
// => {"a":2}
getter("a")({ a: 2 })
// => 2
getter(["a", "b"])({ a: 2, b: 3 })
// => [2,3]
getter({ x: "a", z: "b" })({ a: 2, b: 3 })
// => {"x":2,"z":3}
getter(a => a + 2)(0)
// => 2
getter([null, null])(0)
// => [2,4]
getter({})(0)
// => {"a":2,"b":4}

group_by

group_by([1, 1, 2, 3])
// => {"1":[1,1],"2":[2],"3":[3]}
group_by([{ a: 2 }, { a: 3 }, { a: 2, b: 4 }], "a")
// => {"2":[{"a":2},{"a":2,"b":4}],"3":[{"a":3}]}
group_by([{ a: 2 }, { a: 3 }, { a: 2, b: 4 }], v => v.a)
// => {"2":[{"a":2},{"a":2,"b":4}],"3":[{"a":3}]}
group_by({ a: 1, b: 1, c: 2, d: 3 })
// => {"1":{"a":1,"b":1},"2":{"c":2},"3":{"d":3}}
group_by({ x: { a: 2 }, y: { a: 3 }, z: { a: 2, b: 4 } }, "a")
// => {"2":{"x":{"a":2},"z":{"a":2,"b":4}},"3":{"y":{"a":3}}}
group_by({ x: { a: 2 }, y: { a: 3 }, z: { a: 2, b: 4 } }, v => v.a)
// => {"2":{"x":{"a":2},"z":{"a":2,"b":4}},"3":{"y":{"a":3}}}
group_by(
    [1, 1, 2, 2, 3],
    v => v,
    array => array.reduce((a, b) => a + b, 0)
)
// => {"1":2,"2":4,"3":3}

identity

identity(val)

  • gives val (the identity operation)
identity()
// => null
identity(2)
// => 2
identity([1, 2, 3])
// => [1,2,3]

is_array

is_array("a")
// => false
is_array(0)
// => false
is_array([])
// => true
is_array({})
// => false
is_array(null)
// => false
is_array()
// => false

is_function

is_function("a")
// => false
is_function(0)
// => false
is_function([])
// => false
is_function({})
// => false
is_function(null)
// => false
is_function()
// => false
is_function()
// => false
is_function(() => 1)
// => true
is_function(function () {})
// => true

is_iterator

is_iterator([1, 2, 3])
// => false
function* iter() {
    yield 1
}
is_iterator(iter())
// => true

is_null

is_null("a")
// => false
is_null(0)
// => false
is_null([])
// => false
is_null({})
// => false
is_null(null)
// => true
is_null()
// => true

is_object

is_object("a")
// => false
is_object(0)
// => false
is_object([])
// => false
is_object({})
// => true
is_object({ 3: 1, 4: 2, 5: 3 })
// => true
is_object(null)
// => false
is_object()
// => false

items

items({})
// => []
items({ a: 1 })
// => [["a",1]]
items({ a: 1 }, { b: 2 })
// => [["a",1],["b",2]]
items({ a: 1 }, { b: 2 }, { c: 3 })
// => [["a",1],["b",2],["c",3]]
items({ a: 1, b: 2 }, { b: 3 })
// => [["a",1],["b",2],["b",3]]

key_drop

key_drop({ a: "b", c: "d", e: "f", k: "p" }, "a")
// => {"c":"d","e":"f","k":"p"}
key_drop({ a: "b", c: "d", e: "f", k: "p" }, ["c", "k"])
// => {"a":"b","e":"f"}
key_drop({ a: "b", c: "d", e: "f", k: "p" }, ["a", "c", "z"])
// => {"e":"f","k":"p"}
key_drop({ a: "b", c: "d", e: "f", k: "p" })
// => {"a":"b","c":"d","e":"f","k":"p"}

key_take

key_take({ a: "b", c: "d", e: "f" }, "a")
// => {"a":"b"}
key_take({ a: "b", c: "d", e: "f" }, ["a", "c"])
// => {"a":"b","c":"d"}
key_take({ a: "b", c: "d", e: "f" }, ["a", "c", "z"])
// => {"a":"b","c":"d"}
key_take({ a: "b", c: "d", e: "f" })
// => {"a":"b","c":"d","e":"f"}
key_take({ a: "b", c: "d", e: "f" }, ["a", "c", "z", "j"], 3)
// => {"a":"b","c":"d","z":3,"j":3}
key_take({ a: "b", c: "d", e: "f" }, ["a", "c", "z", "j"], null)
// => {"a":"b","c":"d","z":null,"j":null}

keys

keys({ a: "b", c: "d", e: "f" })
// => ["a","c","e"]

last

last(1)
// => 1
last([1, 2, 3])
// => 3
last({ a: 1, b: 2 })
// => 2

length

length()
// => 0
length(1)
// => 1
length("foo")
// => 1
length([1])
// => 1
length([1, 2, 3])
// => 3
length({ a: 2 })
// => 1
length({ a: 2, b: 3 })
// => 2

map

map(v => v + 2, [1, 2, 3])
// => [3,4,5]
map(v => v + 2, 1)
// => [3]
map(v => v + 2, { a: 3, b: 5 })
// => {"a":5,"b":7}
map((v, index) => v + index, [1, 2, 3])
// => [1,3,5]
map((v, index) => v + index, 1)
// => [1]
map((v, index) => v + index, { a: 3 })
// => {"a":"3a"}

merge

merge()
// => {}
merge({ a: 1 })
// => {"a":1}
merge({ a: 1 }, { b: 2 })
// => {"a":1,"b":2}
merge({ a: 1 }, { b: 2 }, { c: 3 })
// => {"a":1,"b":2,"c":3}
merge({ a: 1, b: 2 }, { b: 3 })
// => {"a":1,"b":3}

nest

nest(s => s + "b", "a", 0)
// => "a"
nest(s => s + "b", "a", 1)
// => "ab"
nest(s => s + "b", "a", 5)
// => "abbbbb"

object_map

object_map(
    v => v,
    v => v + 2,
    [1, 2, 3]
)
// => {"1":3,"2":4,"3":5}
object_map(
    v => v,
    v => v + 2,
    1
)
// => {"1":3}
object_map(
    (v, i) => i + 1,
    (v, i) => v + 2,
    [4, 5, 6]
)
// => {"1":6,"2":7,"3":8}
object_map(
    (v, i) => i + 1,
    (v, i) => v + 2,
    3
)
// => {"1":5}
object_map(
    (v, i) => i + 1,
    (v, i) => v + 2,
    {}
)
// => {"1":3,"2":4}

ordered_object

ordered_object([["a", 2]])
// => {"a":2}
ordered_object([
    ["z", 2],
    ["b", 3],
])
// => {"z":2,"b":3}
ordered_object([
    ["z", 2],
    ["b", 3],
    ["b", 4],
])
// => {"z":2,"b":4}

part

part(
    {
        person: {
            "pk.0": { pk: "pk.0", full_name: "Deanna Gardner" },
            "pk.1": { pk: "pk.1", full_name: "Brian Rodriguez" },
            "pk.2": { pk: "pk.2", full_name: "Rebecca Burke" },
            "pk.3": { pk: "pk.3", full_name: "Brian Taylor" },
            "pk.4": { pk: "pk.4", full_name: "Heather Watkins" },
            "pk.5": { pk: "pk.5", full_name: "Chris Villegas" },
            "pk.6": { pk: "pk.6", full_name: "Danielle Henderson" },
            "pk.7": { pk: "pk.7", full_name: "Matthew Ramirez" },
        },
        transaction: {
            "pk.0": { pk: "pk.0", amount: 370 },
            "pk.1": { pk: "pk.1", amount: 930 },
            "pk.2": { pk: "pk.2", amount: 740 },
            "pk.3": { pk: "pk.3", amount: 560 },
        },
    },
    "person",
    "pk.0",
    "full_name"
)
// => ["Deanna Gardner"]
part(
    {
        person: {
            "pk.0": { pk: "pk.0", full_name: "Deanna Gardner" },
            "pk.1": { pk: "pk.1", full_name: "Brian Rodriguez" },
            "pk.2": { pk: "pk.2", full_name: "Rebecca Burke" },
            "pk.3": { pk: "pk.3", full_name: "Brian Taylor" },
            "pk.4": { pk: "pk.4", full_name: "Heather Watkins" },
            "pk.5": { pk: "pk.5", full_name: "Chris Villegas" },
            "pk.6": { pk: "pk.6", full_name: "Danielle Henderson" },
            "pk.7": { pk: "pk.7", full_name: "Matthew Ramirez" },
        },
        transaction: {
            "pk.0": { pk: "pk.0", amount: 370 },
            "pk.1": { pk: "pk.1", amount: 930 },
            "pk.2": { pk: "pk.2", amount: 740 },
            "pk.3": { pk: "pk.3", amount: 560 },
        },
    },
    "person",
    ["pk.0", "pk.1"],
    "full_name"
)
// => ["Deanna Gardner","Brian Rodriguez"]
part(
    {
        person: {
            "pk.0": { pk: "pk.0", full_name: "Deanna Gardner" },
            "pk.1": { pk: "pk.1", full_name: "Brian Rodriguez" },
            "pk.2": { pk: "pk.2", full_name: "Rebecca Burke" },
            "pk.3": { pk: "pk.3", full_name: "Brian Taylor" },
            "pk.4": { pk: "pk.4", full_name: "Heather Watkins" },
            "pk.5": { pk: "pk.5", full_name: "Chris Villegas" },
            "pk.6": { pk: "pk.6", full_name: "Danielle Henderson" },
            "pk.7": { pk: "pk.7", full_name: "Matthew Ramirez" },
        },
        transaction: {
            "pk.0": { pk: "pk.0", amount: 370 },
            "pk.1": { pk: "pk.1", amount: 930 },
            "pk.2": { pk: "pk.2", amount: 740 },
            "pk.3": { pk: "pk.3", amount: 560 },
        },
    },
    null,
    "pk.0",
    "pk"
)
// => ["pk.0","pk.0"]
part(
    {
        person: {
            "pk.0": { pk: "pk.0", full_name: "Deanna Gardner" },
            "pk.1": { pk: "pk.1", full_name: "Brian Rodriguez" },
            "pk.2": { pk: "pk.2", full_name: "Rebecca Burke" },
            "pk.3": { pk: "pk.3", full_name: "Brian Taylor" },
            "pk.4": { pk: "pk.4", full_name: "Heather Watkins" },
            "pk.5": { pk: "pk.5", full_name: "Chris Villegas" },
            "pk.6": { pk: "pk.6", full_name: "Danielle Henderson" },
            "pk.7": { pk: "pk.7", full_name: "Matthew Ramirez" },
        },
        transaction: {
            "pk.0": { pk: "pk.0", amount: 370 },
            "pk.1": { pk: "pk.1", amount: 930 },
            "pk.2": { pk: "pk.2", amount: 740 },
            "pk.3": { pk: "pk.3", amount: 560 },
        },
    },
    "person",
    ["pk.1", "pk.7"]
)
// => [{"pk":"pk.1","full_name":"Brian Rodriguez"},{"pk":"pk.7","full_name":"Matthew Ramirez"}]

random_choice

random_integer

random_real

range

range(0)
// => []
range(5)
// => [0,1,2,3,4]
range(0, 0)
// => [0]
range(1, 5)
// => [1,2,3,4,5]
range(0, 4)
// => [0,1,2,3,4]
range(0, 4, 2)
// => [0,2,4]

repeat

repeat(1)
// => [1]
repeat(1, 3)
// => [1,1,1]
repeat(1, 0)
// => []

riffle

riffle(iterable, separator)

  • riffle separator between successive elements in a list
riffle([1, 2, 3], "s")
// => [1,"s",2,"s",3]
riffle([1], "s")
// => [1]
riffle([], "s")
// => []

scan

scan(v => console.log(a), [1, 2, 3])
1
2
3
// =>
scan(v => console.log(a), { a: 2, b: 3, c: 4 })
2
3
4
// =>
scan((v, i) => console.log(i), [1, 2, 3])
0
1
2
// =>
scan((v, i) => console.log(i), { a: 2, b: 3, c: 4 })
a
b
c
// =>

sort

sort([3, 1, 2])
// => [1,2,3]
sort([3, 1, 2], (a, b) => a > b)
// => [1,2,3]
sort([3, 1, 2], (a, b) => a < b)
// => [3,2,1]
sort({ a: 3, b: 1, c: 2 })
// => [1,2,3]
sort({ a: 3, b: 1, c: 2 }, (a, b) => a > b)
// => [1,2,3]
sort({ a: 3, b: 1, c: 2 }, (a, b) => a < b)
// => [3,2,1]
// => [3,1,2]
// => {"a":3,"b":1,"c":2}

sort_by

sort_by([4, 3, 10, 2])
// => [2,3,4,10]
sort_by(
    [
        [2, "b"],
        [1, "a"],
    ],
    arr => arr[0]
)
// => [[1,"a"],[2,"b"]]
sort_by(
    [
        [2, 3],
        [2, 1],
        [1, 1],
    ],
    [null, null]
)
// => [[1,1],[2,1],[2,3]]
sort_by(
    [
        [2, 3],
        [2, 1],
        [1, 1],
    ],
    [0, 1]
)
// => [[1,1],[2,1],[2,3]]
sort_by([{ a: 2 }, { a: 3 }, { a: 1 }], "a")
// => [{"a":1},{"a":2},{"a":3}]
sort_by(
    [
        { a: 2, b: 3 },
        { a: 2, b: 1 },
        { a: 1, b: 1 },
    ],
    ["a", "b"]
)
// => [{"a":1,"b":1},{"a":2,"b":1},{"a":2,"b":3}]

to_array

to_array(0)
// => [0]
to_array("a")
// => ["a"]
to_array([0, 1, 2, [3, 4], { a: 5 }])
// => [0,1,2,[3,4],{"a":5}]
to_array({ a: 1, b: 2, c: [3, 4], d: { a: 5 } })
// => [1,2,[3,4],{"a":5}]
to_array()
// => []
to_array("", 0, 1, 2, 3)
// => ["",0,1,2,3]
to_array("", 0, 1, [2, 3], 4)
// => ["",0,1,2,3,4]
to_array("", 0, 1, [2, 3], { a: 4, b: 5 })
// => ["",0,1,2,3,4,5]

values

values({ a: "b", c: "d", e: "f" })
// => ["b","d","f"]

zip

zip(array1, array2, ...)

  • returns [[array10, array20, ...], [array11, array21, ...], ...]
zip([1, 2, 3], [4, 5, 6])
// => [[1,4],[2,5],[3,6]]
1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.3.1

2 years ago

1.3.0

3 years ago

1.2.10

4 years ago

1.2.9

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.3

5 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago