3 people like it.

Simple JS Calculator

Simple calculator application for adding numbers, compiles to JavaScript via Pit (v0.1) (http://pitfw.posterous.com). Run: http://trelford.com/PitCalculatorApp.htm

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
namespace Pit

open Pit
open Pit.Dom

module Calculator =

    let [<Js>] (?) (el:DomElement) name =
        el.GetAttribute(name) 
    let [<Js>] (?<-) (el:DomElement) name value =
        el.SetAttribute(name,value)
    let [<Js>] tag name attributes =
        let el = document.CreateElement(name)
        for (name,value) in attributes do
            el.SetAttribute(name,value)
        el

    let [<Js>] display = tag "input" ["type","text";"value","0"]
    let [<Js>] mutable operation = None
    let [<Js>] mutable append = false
    
    let [<Js>] digit d =
        let s = d.ToString()       
        s, fun () -> 
            let value = display?value
            if append then display?value <- value + s
            else display?value <- s; append <- true                

    let [<Js>] calculate () =       
        let value = int display?value        
        operation |> Option.iter (fun op ->
            let newValue = op value           
            display?value <- newValue.ToString() 
        )
        operation <- None

    let [<Js>] add () = 
        calculate ()
        append <- false
        let value = display?value |> int
        operation <- (+) value |> Some
        
    let [<Js>] total () = calculate ()          

    let [<Js>] buttons =
        [[digit 7; digit 8; digit 9]
         [digit 4; digit 5; digit 6]
         [digit 1; digit 2; digit 3]
         [digit 0; "+", add; "=", total]]               

    [<DomEntryPoint>]    
    let [<Js>] main() =                                                                                          
        let createRow row =
            let createCell (text,action) =
                let td = tag "td" []
                let input = tag "input" ["type","button";"value",text]         
                input |> Event.click |> Event.add (fun _ -> action ())      
                td.AppendChild input
                td
            let tr = tag "tr" [] 
            for cell in row do cell |> createCell |> tr.AppendChild 
            tr      
        let table = (tag "table" [] |> DomTable.Of)  
        let td = tag "td" ["colspan","3"]
        td.AppendChild display
        let tr = tag "tr" []
        tr.AppendChild td 
        table.AppendChild tr 
        for row in buttons do
            row |> createRow |> table.AppendChild
        let div = document.GetElementById "calculator"
        div.AppendChild table

// Note: add this div to body of project's HTML page: <div id="calculator"></div>
namespace Pit
module Calculator

from Pit
val el : 'a
val name : 'a
val value : 'a
val tag : name:'a -> attributes:seq<'b * 'c> -> 'd

Full name: Pit.Calculator.tag
val attributes : seq<'a * 'b>
val display : obj

Full name: Pit.Calculator.display
val mutable operation : (int -> int) option

Full name: Pit.Calculator.operation
union case Option.None: Option<'T>
val mutable append : bool

Full name: Pit.Calculator.append
val digit : d:'a -> string * (unit -> unit)

Full name: Pit.Calculator.digit
val d : 'a
val s : string
System.Object.ToString() : string
val value : string
val calculate : unit -> unit

Full name: Pit.Calculator.calculate
val value : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
module Option

from Microsoft.FSharp.Core
val iter : action:('T -> unit) -> option:'T option -> unit

Full name: Microsoft.FSharp.Core.Option.iter
val op : (int -> int)
val newValue : int
System.Int32.ToString() : string
System.Int32.ToString(provider: System.IFormatProvider) : string
System.Int32.ToString(format: string) : string
System.Int32.ToString(format: string, provider: System.IFormatProvider) : string
val add : unit -> unit

Full name: Pit.Calculator.add
union case Option.Some: Value: 'T -> Option<'T>
val total : unit -> unit

Full name: Pit.Calculator.total
val buttons : (string * (unit -> unit)) list list

Full name: Pit.Calculator.buttons
val main : unit -> 'a

Full name: Pit.Calculator.main
val createRow : (seq<string * (unit -> unit)> -> 'a)
val row : seq<string * (unit -> unit)>
val createCell : (string * (unit -> unit) -> 'a)
val text : string
val action : (unit -> unit)
val td : 'a
val input : obj
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
val add : callback:('T -> unit) -> sourceEvent:IEvent<'Del,'T> -> unit (requires delegate and 'Del :> System.Delegate)

Full name: Microsoft.FSharp.Control.Event.add
val tr : 'a
val cell : string * (unit -> unit)
val table : 'a
val row : (string * (unit -> unit)) list
val div : 'a
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/93
Posted:13 years ago
Author:Phillip Trelford
Tags: js , pit