Permalink
Browse files

Update examples to match the new Char, List, and HTTP APIs.

  • Loading branch information...
Evan Czaplicki
Evan Czaplicki committed Oct 4, 2012
1 parent 10db525 commit 4407a66a644d564365607c97676d8185fa09e981
Showing with 27 additions and 17 deletions.
  1. +1 −1 public/examples/Reactive/KeepIf.elm
  2. +26 −16 public/examples/Reactive/ZipCodes.elm
@@ -3,7 +3,7 @@ import Signal.Input
(field, input) = textField ""
-isValid s = exists ((==) 'a') s
+isValid s = any ((==) 'a') s
scene inp =
field `above` plainText ("Last input that contained an 'a' was: " ++ inp)
@@ -1,25 +1,35 @@
-import Signal.HTTP (gets)
+import Data.Char
+import Data.Maybe
+import Signal.HTTP
import Signal.Input (textField)
-valid s = length s == 5 && onlyDigits s
-onlyDigits = let digits = ['0','1','2','3','4','5','6','7','8','9'] in
- forall (\c -> exists ((==)c) digits)
+(field,rawInput) = textField "Zip Code"
-url = "http://zip.elevenbasetwo.com/v2/US/"
+-- Covert raw input into a usable URL.
+toUrl s = if length s == 5 && all isDigit s
+ then Just ("http://zip.elevenbasetwo.com/v2/US/" ++ s)
+ else Nothing
-component (fld, txt) =
- let { toUrl s = if valid s then Just (url ++ s) else Nothing
- ; input = plainText "Enter a US Zip Code:" `beside` fld
- ; address = lift display . gets . lift toUrl $ txt
- }
- in lift (above input) address
+-- Transform the signal of raw input into usable data, indicating if the input
+-- is valid and, if so, what it is.
+realInput = lift toUrl rawInput
+-- Send AJAX requests for any valid input!
+responses = sendGet . lift (fromMaybe "") $ keepIf isJust Nothing realInput
+
+-- Display a response.
display response =
- (case response of
- { Just address -> asText address
- ; Nothing -> plainText "Not a valid address. Maybe try 12345."
- })
+ case response of
+ { Success address -> text . monospace $ toText address
+ ; Waiting -> image 16 16 "waiting.gif"
+ ; Failure _ _ -> asText response }
+
+-- Give the user a message depending on whether their input is valid and
+-- the response from any AJAX requests.
+message =
+ let msg = plainText "Enter a valid zip code, such as 12345 or 90210." in
+ lift2 (\i r -> maybe msg (\_ -> display r) i) realInput responses
-main = component (textField "")
+main = lift (above field) message

0 comments on commit 4407a66

Please sign in to comment.