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.0.6.20 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.19 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.18 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.17 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.16 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.15 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.14 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.13 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.12 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.11 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.10 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.9 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.8 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.7 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.6 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.5 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.4 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.3 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.2 tracks/lfe/exercises/bank-account/src/example.lfe
trackler-2.0.6.1 tracks/lfe/exercises/bank-account/src/example.lfe