This commit is contained in:
Jonathan Leonard 2012-11-25 20:39:58 -08:00
Родитель d92d208a45
Коммит 1c85d0b38a
4 изменённых файлов: 258 добавлений и 0 удалений

Просмотреть файл

@ -392,6 +392,14 @@ Elixir:
- .ex
- .exs
Elm:
type: programming
lexer: Haskell
group: Haskell
primary_extension: .elm
extensions:
- .elm
Emacs Lisp:
type: programming
lexer: Scheme

127
samples/Elm/Basic.elm Normal file
Просмотреть файл

@ -0,0 +1,127 @@
import List (intercalate,intersperse)
import Website.Skeleton
import Website.ColorScheme
addFolder folder lst =
let add (x,y) = (x, folder ++ y ++ ".elm") in
let f (n,xs) = (n, map add xs) in
map f lst
elements = addFolder "Elements/"
[ ("Primitives",
[ ("Text" , "HelloWorld")
, ("Images", "Image")
, ("Fitted Images", "FittedImage")
, ("Videos", "Video")
, ("Markdown", "Markdown")
])
, ("Formatting",
[ ("Size" , "Size")
, ("Opacity" , "Opacity")
, ("Text" , "Text")
, ("Typeface", "Typeface")
])
, ("Layout",
[ ("Simple Flow", "FlowDown1a")
, ("Flow Down" , "FlowDown2")
, ("Layers" , "Layers")
, ("Positioning", "Position")
, ("Spacers" , "Spacer")
])
, ("Collage", [ ("Lines" , "Lines")
, ("Shapes" , "Shapes")
, ("Sprites" , "Sprite")
, ("Elements" , "ToForm")
, ("Colors" , "Color")
, ("Textures" , "Texture")
, ("Transforms", "Transforms")
])
]
functional = addFolder "Functional/"
[ ("Recursion",
[ ("Factorial" , "Factorial")
, ("List Length", "Length")
, ("Zip" , "Zip")
, ("Quick Sort" , "QuickSort")
])
, ("Functions",
[ ("Anonymous Functions", "Anonymous")
, ("Application" , "Application")
, ("Composition" , "Composition")
, ("Infix Operators" , "Infix")
])
, ("Higher-Order",
[ ("Map" , "Map")
, ("Fold" , "Sum")
, ("Filter" , "Filter")
, ("ZipWith", "ZipWith")
])
, ("Data Types",
[ ("Maybe", "Maybe")
, ("Boolean Expressions", "BooleanExpressions")
, ("Tree", "Tree")
])
]
reactive = addFolder "Reactive/"
[ ("Mouse", [ ("Position", "Position")
, ("Presses" , "IsDown")
, ("Clicks" , "CountClicks")
, ("Position+Image", "ResizeYogi")
, ("Position+Collage" , "Transforms")
-- , ("Hover" , "IsAbove")
])
,("Keyboard",[ ("Keys Down" , "KeysDown")
, ("Key Presses", "CharPressed")
])
, ("Window", [ ("Size", "ResizePaint")
, ("Centering", "Centering")
])
, ("Time", [ ("Before and After", "Between")
, ("Every" , "Every")
, ("Clock" , "Clock")
])
, ("Input", [ ("Text Fields", "TextField")
, ("Passwords" , "Password")
, ("Check Boxes", "CheckBox")
, ("String Drop Down", "StringDropDown")
, ("Drop Down", "DropDown")
])
, ("Random", [ ("Randomize", "Randomize") ])
, ("HTTP", [ ("Zip Codes", "ZipCodes") ])
, ("Filters",[ ("Sample", "SampleOn")
, ("Keep If", "KeepIf")
, ("Drop Repeats", "DropRepeats")
])
]
example (name, loc) = Text.link ("/edit/examples/" ++ loc) (toText name)
toLinks (title, links) =
flow right [ width 130 (text $ toText " " ++ italic (toText title))
, text (intercalate (bold . Text.color accent4 $ toText " · ") $ map example links)
]
insertSpace lst = case lst of { x:xs -> x : spacer 1 5 : xs ; [] -> [] }
subsection w (name,info) =
flow down . insertSpace . intersperse (spacer 1 1) . map (width w) $
(text . bold $ toText name) : map toLinks info
words = [markdown|
### Basic Examples
Each example listed below focuses on a single function or concept.
These examples demonstrate all of the basic building blocks of Elm.
|]
content w =
words : map (subsection w) [ ("Display",elements), ("React",reactive), ("Compute",functional) ]
exampleSets w = flow down . map (width w) . intersperse (plainText " ") $ content w
main = lift (skeleton exampleSets) Window.width

32
samples/Elm/QuickSort.elm Normal file
Просмотреть файл

@ -0,0 +1,32 @@
main = asText (qsort [3,9,1,8,5,4,7])
qsort lst =
case lst of
x:xs -> qsort (filter ((>=)x) xs) ++ [x] ++ qsort (filter ((<)x) xs)
[] -> []
{---------------------
QuickSort works as follows:
- Choose a pivot element which be placed in the "middle" of the sorted list.
In our case we are choosing the first element as the pivot.
- Gather all of the elements less than the pivot (the first filter).
We know that these must come before our pivot element in the sorted list.
Note: ((>=)x) === (\y -> (>=) x y) === (\y -> x >= y)
- Gather all of the elements greater than the pivot (the second filter).
We know that these must come after our pivot element in the sorted list.
- Run `qsort` on the lesser elements, producing a sorted list that contains
only elements less than the pivot. Put these before the pivot.
- Run `qsort` on the greater elements, producing a sorted list. Put these
after the pivot.
Note that choosing a bad pivot can have bad effects. Take a sorted list with
N elements. The pivot will always be the lowest member, meaning that it does
not divide the list very evenly. The list of lessers has 0 elements
and the list of greaters has N-1 elemens. This means qsort will be called
N times, each call looking through the entire list. This means, in the worst
case, QuickSort will make N^2 comparisons.
----------------------}

91
samples/Elm/Tree.elm Normal file
Просмотреть файл

@ -0,0 +1,91 @@
{-----------------------------------------------------------------
Overview: A "Tree" represents a binary tree. A "Node" in a binary
tree always has two children. A tree can also be "Empty". Below
I have defined "Tree" and a number of useful functions.
This example also includes some challenge problems :)
-----------------------------------------------------------------}
data Tree a = Node a (Tree a) (Tree a) | Empty
empty = Empty
singleton v = Node v Empty Empty
insert x tree =
case tree of
Empty -> singleton x
Node y left right ->
if x == y then tree else
if x < y then Node y (insert x left) right
else Node y left (insert x right)
fromList xs = foldl insert empty xs
depth tree =
case tree of
Node v left right -> 1 + max (depth left) (depth right)
Empty -> 0
map f tree =
case tree of
Node v left right -> Node (f v) (map f left) (map f right)
Empty -> Empty
t1 = fromList [1,2,3]
t2 = fromList [2,1,3]
main = flow down [ display "depth" depth t1
, display "depth" depth t2
, display "map ((+)1)" (map ((+)1)) t2
]
display name f v =
text . monospace . toText $
concat [ show (f v), " &lArr; ", name, " ", show v ]
{-----------------------------------------------------------------
Exercises:
(1) Sum all of the elements of a tree.
sum :: Tree Number -> Number
(2) Flatten a tree into a list.
flatten :: Tree a -> [a]
(3) Check to see if an element is in a given tree.
isElement :: a -> Tree a -> Bool
(4) Write a general fold function that acts on trees. The fold
function does not need to guarantee a particular order of
traversal.
fold :: (a -> b -> b) -> b -> Tree a -> b
(5) Use "fold" to do exercises 1-3 in one line each. The best
readable versions I have come up have the following length
in characters including spaces and function name:
sum: 16
flatten: 21
isElement: 46
See if you can match or beat me! Don't forget about currying
and partial application!
(6) Can "fold" be used to implement "map" or "depth"?
(7) Try experimenting with different ways to traverse a
tree: pre-order, in-order, post-order, depth-first, etc.
More info at: http://en.wikipedia.org/wiki/Tree_traversal
-----------------------------------------------------------------}