Sha256: eb9f65461760846f95b8eb68179ff6b31e81727e3d82447522f91868304d8302

Contents?: true

Size: 1.46 KB

Versions: 396

Compression:

Stored size: 1.46 KB

Contents

(defmodule bank-account
  (export (balance 1)
          (charge 2)
          (close 1)
          (create 0)
          (deposit 2)
          (withdraw 2)))

(defun balance (pid) (handle (is_process_alive pid) pid 'balance 0))

(defun charge
  ([pid amount] (when (> amount 0))
   (handle (is_process_alive pid) pid 'charge amount))
  ([_pid _amount] 0))

(defun close (pid) (handle (is_process_alive pid) pid 'close 0))

(defun create () (spawn (lambda () (loop 0))))

(defun deposit
  ([pid amount]   (when (> amount 0)) (! pid `#(deposit ,amount)))
  ([_pid _amount] 'ok))

(defun withdraw
  ([pid amount] (when (> amount 0))
   (handle (is_process_alive pid) pid 'withdraw amount))
  ([_pid _amount] 0))

(defun handle
  (['true pid request argument]
   (! pid `#(,request ,argument ,(self)))
   (receive
     (`#(,request ,answer) answer))))

(defun loop (balance)
  (receive
    (`#(balance ,_argument ,pid)
     (! pid `#(balance, ,balance))
     (loop balance))
    (`#(charge ,amount ,pid)
     (let ((charge (loop-charge balance amount)))
       (! pid `#(charge ,charge))
       (loop (- balance charge))))
    (`#(close ,_argument ,pid)
     (! pid `#(close ,balance)))
    (`#(deposit ,amount)
     (loop (+ balance amount)))
    (`#(withdraw ,amount ,pid)
     (let ((withdrawal (min balance amount)))
       (! pid `#(withdraw ,withdrawal))
       (loop (- balance withdrawal))))))

(defun loop-charge
  ([balance amount]   (when (>= balance amount)) amount)
  ([_balance _amount] 0))

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.179 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.178 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.177 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.176 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.175 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.174 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.173 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.172 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.171 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.170 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.169 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.167 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.166 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.165 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.164 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.163 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.162 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.161 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.2.1.160 tracks/lfe/exercises/bank-account/src/example.lfe