Guest User

Untitled

a guest
Aug 20th, 2018
5,824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main exposing (main)
  2.  
  3. import Html exposing (Html, div, h1, text, textarea)
  4. import Html.Attributes exposing (class, readonly, rows, style, value)
  5. import Html.Events exposing (onInput)
  6.  
  7.  
  8. -- Fibonacci stuff
  9.  
  10.  
  11. fibonacciFy : String -> String -> String
  12. fibonacciFy origIndent input =
  13.  input
  14.   |> String.lines
  15.   |> List.map (fibonacciFyLine origIndent)
  16.   |> String.join "\n"
  17.  
  18.  
  19. fibonacciFyLine : String -> String -> String
  20. fibonacciFyLine origIndent input =
  21.  let
  22.   trimed =
  23.     String.trimLeft input
  24.  
  25.   indentLevel =
  26.     (String.length input - String.length trimed) // String.length origIndent
  27.  in
  28.  String.repeat (cumulativeFib indentLevel) " " ++ trimed
  29.  
  30.  
  31. cumulativeFib : Int -> Int
  32. cumulativeFib n =
  33.  List.range 0 n
  34.   |> List.map fibonacci
  35.   |> List.sum
  36.  
  37.  
  38. fibonacci : Int -> Int
  39. fibonacci n =
  40.  let
  41.   fib a b n =
  42.     if n <= 0 then
  43.        a
  44.     else
  45.        fib b (a + b) (n - 1)
  46.  in
  47.  fib 0 1 n
  48.  
  49.  
  50.  
  51. --
  52.  
  53.  
  54. type Msg
  55.  = SetInput String
  56.  | SetOrigIndent String
  57.  
  58.  
  59. update msg model =
  60.  case msg of
  61.   SetInput input ->
  62.     { model | input = input }
  63.  
  64.   SetOrigIndent ind ->
  65.     { model | origIndent = ind }
  66.  
  67.  
  68. view { input, origIndent } =
  69.  div []
  70.   [ div []
  71.     [ text "Original indent:"
  72.     , Html.input [ onInput SetOrigIndent, value origIndent ] []
  73.     ]
  74.   , div []
  75.     [ h1 [] [ text "Original" ]
  76.     , inputTxt [ onInput SetInput ] input
  77.     ]
  78.   , div []
  79.     [ h1 [] [ text "Fibonaccified" ]
  80.     , inputTxt [ readonly True ] (fibonacciFy origIndent input)
  81.     ]
  82.   ]
  83.  
  84.  
  85. main =
  86.  Html.beginnerProgram
  87.   { model = { origIndent = "  ", input = sample }
  88.   , update = update
  89.   , view = view
  90.   }
  91.  
  92.  
  93.  
  94. --
  95.  
  96.  
  97. inputTxt attrs txt =
  98.  textarea ([ value txt, class "txtInput", rows 17 ] ++ attrs) []
  99.  
  100.  
  101. sample =
  102.  """foo {
  103. bar {
  104. baz {
  105. quz {
  106.  quux {
  107.  corge {
  108.    grault {
  109.    garply {
  110.       // waldoo
  111.    }
  112.    }
  113.  }
  114.  }
  115. }
  116. }
  117. }
  118. }"""
Advertisement
Add Comment
Please, Sign In to add comment