← Back to Playground

Gojju 🌶️ API Reference

All built-in functions and methods

I/O Functions

print(...args)

Print values to stdout.

print("Hello", "World") → Hello World
puts(...args)

Print values with newlines (Ruby-style).

puts("line1", "line2")
gets()

Read a line from stdin.

input(prompt="")

Read input with an optional prompt.

List Functions

head(list)

Get the first element of a list.

head([1, 2, 3]) → 1
tail(list)

Get all but the first element of a list.

tail([1, 2, 3]) → [2, 3]
last(list)

Get the last element of a list.

last([1, 2, 3]) → 3
init(list)

Get all but the last element.

init([1, 2, 3]) → [1, 2]
len(obj)

Get the length of a list or string.

len([1, 2, 3]) → 3
push(list, item)

Add an item to the end of a list (mutating).

pop(list)

Remove and return the last item.

concat(...lists)

Concatenate multiple lists.

concat([1, 2], [3, 4]) → [1, 2, 3, 4]
reverse(list)

Reverse a list.

reverse([1, 2, 3]) → [3, 2, 1]
sort(list)

Sort a list.

sort([3, 1, 2]) → [1, 2, 3]
unique(list)

Remove duplicates from a list.

unique([1, 1, 2, 2, 3]) → [1, 2, 3]
flatten(list)

Flatten a nested list by one level.

flatten([[1, 2], [3, 4]]) → [1, 2, 3, 4]
zip(...lists)

Zip multiple lists together.

zip([1, 2], ["a", "b"]) → [[1, "a"], [2, "b"]]
enumerate(list, start=0)

Return list of [index, value] pairs.

enumerate(["a", "b"]) → [[0, "a"], [1, "b"]]
range(start, end?, step?)

Generate a range of numbers.

range(5) → [0, 1, 2, 3, 4]
range(1, 5) → [1, 2, 3, 4]

Higher-Order Functions

map(func, list)

Apply a function to each element.

[1, 2, 3] |> map(\x -> x * 2) → [2, 4, 6]
filter(func, list)

Filter elements by predicate.

[1, 2, 3, 4] |> filter(\x -> x > 2) → [3, 4]
reduce(func, list, initial?)

Reduce a list to a single value.

each(list, func)

Iterate over each element (Ruby-style).

find(func, list)

Find the first element matching predicate.

any(func, list)

Check if any element matches predicate.

all(func, list)

Check if all elements match predicate.

take(n, list)

Take first n elements.

take(2, [1, 2, 3, 4]) → [1, 2]
drop(n, list)

Drop first n elements.

drop(2, [1, 2, 3, 4]) → [3, 4]
takewhile(func, list)

Take elements while predicate is true.

dropwhile(func, list)

Drop elements while predicate is true.

String Functions

split(str, sep?)

Split a string by separator.

split("a,b,c", ",") → ["a", "b", "c"]
join(list, sep="")

Join a list into a string.

join(["a", "b"], "-") → "a-b"
upper(str)

Convert to uppercase.

"hello".upper() → "HELLO"
lower(str)

Convert to lowercase.

"HELLO".lower() → "hello"
capitalize(str)

Capitalize first letter.

strip(str)

Remove leading/trailing whitespace.

replace(str, old, new)

Replace occurrences in a string.

chars(str)

Split string into characters.

chars("abc") → ["a", "b", "c"]
words(str)

Split string into words.

lines(str)

Split string into lines.

match(pattern, str)

Match regex pattern against string.

gsub(pattern, replacement, str)

Replace all regex matches (global substitution).

Math Functions

abs(x)

Absolute value.

abs(-5) → 5
min(...args)

Minimum value.

min([5, 2, 8]) → 2
max(...args)

Maximum value.

max([5, 2, 8]) → 8
sum(list)

Sum of a list.

sum([1, 2, 3, 4, 5]) → 15
product(list)

Product of a list.

product([1, 2, 3, 4]) → 24
sqrt(x)

Square root.

sqrt(16) → 4.0
pow(x, y)

Power.

pow(2, 10) → 1024
floor(x)

Floor of a number.

ceil(x)

Ceiling of a number.

round(x, n=0)

Round a number.

Type Functions

type(x)

Get the type of a value.

type(42) → "integer"
type([1,2,3]) → "list"
int(x)

Convert to integer.

float(x)

Convert to float.

str(x)

Convert to string.

list(x)

Convert to list.

bool(x)

Convert to boolean.

FP Utilities

id(x)

Identity function - returns x unchanged.

id(42) → 42
const(x)

Create a function that always returns x.

const(5)(99) → 5
compose(f, g)

Compose two functions: compose(f, g)(x) = f(g(x))

flip(f)

Flip argument order of a binary function.

flip(pow)(2, 3) → 9 (= pow(3, 2))
curry(f)

Curry a binary function.

partial(f, ...args)

Partially apply arguments to a function.

times(n, func)

Execute a function n times.

times(3, \i -> i * 2) → [0, 2, 4]