The GLEAM language, example sqlite database.

In the list of interesting languages there is GLEAM.
Just like ELIXIR it runs on BEAM. But has static compile time type checking.

First install packages,
Code:
pkg install rebar3
pkg install gleam

Make new gleam project
Code:
gleam new demo1

Code:
cd demo1
mkdir ./src

Edit file ./src/demo1.gleam

Code:
import gleam/dynamic/decode
import gleam/io
import gleam/int
import sqlight

pub type Person {
  Person(name: String, age: Int)
}

fn print_people(people: List(Person)) -> Nil {
  case people {
    [] -> Nil
    [person, ..rest] -> {
      echo "  Name: " <> person.name <> "  Age: " <> int.to_string(person.age)
      print_people(rest)
    }
  }
}

pub fn main() {
  let person = Person(name: "Jake", age: 20)
  let updated_person = Person(..person, name: "John")
  updated_person.name |> io.println

  use conn <- sqlight.with_connection("data.db")

  let create_table_sql = "
    create table if not exists people (
      name text not null,
      age integer not null
    );
  "
  let assert Ok(Nil) = sqlight.exec(create_table_sql, conn)

  let insert_sql = "insert into people (name, age) values (?, ?);"

  let assert Ok([]) = sqlight.query(
    insert_sql,
    on: conn,
    with: [sqlight.text("Jake"), sqlight.int(20)],
    expecting: decode.dynamic,
  )
  let assert Ok([]) = sqlight.query(
    insert_sql,
    on: conn,
    with: [sqlight.text("John"), sqlight.int(25)],
    expecting: decode.dynamic,
  )
  let person_decoder = {
    use name <- decode.field(0, decode.string)
    use age <- decode.field(1, decode.int)
    decode.success(Person(name: name, age: age))
  }

  let select_sql = "select name, age from people;"

  let assert Ok(people) = sqlight.query(
    select_sql,
    on: conn,
    with: [],
    expecting: person_decoder,
  )
  print_people(people)
}

create a file ./runme
sh:
export CFLAGS="-Wno-error=implicit-function-declaration"
export CC=gcc
export CXX=g++
gleam add sqlight
gleam run

./runme

That's it. [See you later alligator]
 
Back
Top