Skip to main content

Command Palette

Search for a command to run...

A bit of Golo for the weekend

Published
3 min read

So, the GoloScript interpreter isn't completely "done" yet, but we can already do some cool things with it.

Here are a few examples.

Functions are first-class citizens

With a bit of currying, some functional programming...

module functions.ListMapFunc

function addition = |a, b| {
  return a + b
}

function main = |args| {
  # function as output
  let add = |a| -> |b| -> addition(a, b)

  let addOne = add(1)
  println(addOne(41)) # Should print 42

  # addOne: function as input
  println(
    list[1, 2, 3, 4, 5]
      : map(addOne)
      : map(addOne)
  )
  # Should print [3, 4, 5, 6, 7]
}

A functional Kebab preparation

module no.Iteration

function main = |args| {
  let noChips = |food| -> food isnt "🍟"
  # returns true if food is not chips
  let noOnion = |food| -> food isnt "🧅"
  # returns true if food is not onion

  # Test with is operator
  println("🍟" is "🍟")  # true
  println("🍟" is "🧅")  # false

  let cutFood = |food| -> "pieces of " + food

  let Food = list["🍞", "🥬", "🍅", "🍖", "🍟", "🧅"]

  # My kebab recipe without chips and onion
  let myKebabRecipe = Food
    : filter(noChips)
    : filter(noOnion)
    : map(cutFood)

  println("Kebab recipe ingredients:", myKebabRecipe)
  # Should print:
  # Kebab recipe ingredients: ["pieces of 🍞", "pieces of 🥬", "pieces of 🍅", "pieces of 🍖"]

  # Reduce to a single string
  let mixFood = |accFood, nextFood| -> accFood + nextFood + " "

  let kebab = myKebabRecipe
    : reduce("🥙 with: ", mixFood)

  println("Final Kebab:", kebab)
}

So we'll get a nice kebab without chips or onions!

true
false
Kebab recipe ingredients: [pieces of 🍞, pieces of 🥬, pieces of 🍅, pieces of 🍖]
Final Kebab: 🥙 with: pieces of 🍞 pieces of 🥬 pieces of 🍅 pieces of 🍖

And to finish, "good or bad" with augmented Union types

module yum.Yum

function main = |args| {

  union SomeThing = {
    Yum = { value }  # good
    Yuck = { value } # bad
  }

  augment SomeThing$Yum {
    function so = |this, ifYum, ifYuck| -> ifYum(this: value())
  }

  augment SomeThing$Yuck {
    function so = |this, ifYum, ifYuck| -> ifYuck(this: value())
  }

  # you're good or bad but not both
  let banana = SomeThing.Yum("🍌")
  let hotPepper = SomeThing.Yuck("🌶️")

  println(banana)
  println(hotPepper)

  println(banana: value()) # 🍌
  println(hotPepper: value()) # 🌶️

  println(banana: isYum()) # true
  println(banana: isYuck()) # false

  # we want only bananas
  let onlyBananas = |value| {
    if value is "🍌" { return SomeThing:Yum(value) }
    return SomeThing:Yuck("🌶️")
  }

  # test onlyBananas function
  if onlyBananas("🍌"): isYum() {
    println("I 💙 bananas!")
  } else {
    println("💔 I don't like this!")
  }

  # test onlyBananas with the augmentation
  onlyBananas("🌶️"): so(
    |v| -> println("💙 Yum! I got a " + v),
    |v| -> println("💔 Yuck! I got a " + v)
  )

}

Which should give us:

SomeThing.Yum(value=🍌)
SomeThing.Yuck(value=🌶️)
🍌
🌶️
true
false
I 💙 bananas!
💔 Yuck! I got a 🌶️

Nice, right?

the playground is up to date with these examples: https://codeberg.org/TypeUnsafe/golo-playground

Next time we'll talk about error handling and exceptions in Golo!

Have a great weekend!