nixpulvis / oursh

Your comrade through the perilous world of UNIX.
http://nixpulvis.com/oursh/oursh
MIT License
67 stars 6 forks source link

Functions #43

Open nixpulvis opened 5 years ago

nixpulvis commented 5 years ago

POSIX shell language defines functions like foo() { ... }. We must support them.

Related to #27.

nixpulvis commented 5 years ago

Can be used with #! blocks.

fib() {#!/usr/bin/env elixir
    defmodule Math do
      def fibfast(n) do fib_acc(1, 0, n) end
      def fib_acc(a, b, 0) do a + b end
      def fib_acc(a, b, n) do fib_acc(b, a+b, n-1) end

      def fibslow(0) do 1 end
      def fibslow(1) do 1 end
      def fibslow(n) do fibslow(n-1) + fibslow(n-2) end
    end

    print_fib = fn(n) ->
      n |> Math.fibfast() |> IO.puts()
    end

    print_fib.($1)
}

fib 10