A series of exercises to get a feel for how generic types and type variables work in Elm.
For each of these, you are given some code that is not compiling. Your task is to get it
compiling. You can change the body of the function but not the signature. No cheating
with Debug.todo or infinite recursions either 😉
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Add | |
| def self.mempty | |
| new(0) | |
| end | |
| def initialize(number) | |
| @number = number | |
| end | |
| attr_reader :number |
There's a lot of type terminology and jargon going around when discussing types in Elm. This glossary attempts to list some of the most common type terms along with synonyms, terms from other language communities, examples, and links to more detailed articles on each topic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -e | |
| echo "== BUILDING THE APP ==" | |
| yarn parcel build src/index.html | |
| echo "== CONFIGURING REDIRECTS ==" | |
| if [ "$CONTEXT" = "production" ]; then | |
| cp production_redirects dist/_redirects |
Redundant if/else - identity
def can_edit?
if admin?
true
else
false
end
end
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| puts "4 line REPL (read eval print loop)" | |
| $stdin.each_line do |line| # READ | |
| return_value = eval(line.chomp) # EVAL | |
| puts return_value # PRINT | |
| end # LOOP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| view : Model -> Html Msg | |
| view model = | |
| div [] | |
| [ div [ class "icon" ] [ span [class "icon-profile" ] [] ] | |
| , div [ class "icon" ] [ span [ class "icon-clock" ] [] ] | |
| ] | |
| -- REFACTOR TO | |
| view : Model -> Html Msg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class NonEmptyArray | |
| include Enumerable | |
| def initialize(first, rest) | |
| @first = first | |
| @rest = rest | |
| end | |
| def first | |
| @first |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def add(number1, number2) | |
| number2.times.reduce(number1) { |total| total.next } | |
| end | |
| add(2,3) | |
| # => 5 | |
| def subtract(number1, number2) | |
| number2.times.reduce(number1) { |total| total.pred } | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Hamming | |
| def self.zip_count(s1, s2) | |
| s1.chars. | |
| zip(s2.chars). | |
| count { |c1, c2| c1 != c2 } | |
| end | |
| def self.for_loop(s1, s2) | |
| distance = 0 |