Skip to content

Instantly share code, notes, and snippets.

Avatar

Eric Normand ericnormand

View GitHub Profile
@ericnormand
ericnormand / 00 Valid names.md
Last active Jan 27, 2021
412 PurelyFunctional.tv Newsletter
View 00 Valid names.md

Valid names

This challenge looks like a fun experiment in building a simple rules-based validator.

Definitions:

  1. A name is a sequence of terms separated by a space. It must have at least 2 terms. The last term must be a word.
  2. A term is either an initial or a word.
  3. An initial is a single capital letter followed by a period.
  4. A word is a capital letter followed by one or more letters (upper or lower case).
@ericnormand
ericnormand / 00 Least common multiple.md
Created Jan 19, 2021
411 PurelyFunctional.tv Newlsetter
View 00 Least common multiple.md

Least common multiple

Find the least common multiple of an array of integers. That is, find the smallest positive integer that is evenly divisible by all of the integers in the array.

Examples

(lcm [1 2 3]) ;=> 6
(lcm [5 4 4]) ;=> 20
(lcm []) ;=> 1
@ericnormand
ericnormand / 00 Nested lists.md
Created Jan 11, 2021
410 - PurelyFunctional.tv Newsletter
View 00 Nested lists.md

Nested lists

Write a function that nests the elements of a list one level deeper by repeating that element inside a new list a given number of times.

Examples

(nest [:a :b :c] 2) ;=> ((:a :a) (:b :b) (:c :c))
(nest [] 10) ;=> ()
(nest [1 2 3 4] 1) ;=> ((1) (2) (3) (4))
@ericnormand
ericnormand / 00 First before second.md
Created Jan 4, 2021
409 PurelyFunctional.tv Newsletter
View 00 First before second.md

First before second

Write a function that takes a string and two letters. The function should return true if every instance of the first letter comes before every instance of the second letter.

Examples

(first-before? "A rabbit jumps joyfully" \a \j) ;=> true
(first-before? "A jolly rabbit jumps joyfully" \a \j) ;=> false
(first-before? "Don't tread on me" \t \m) ;=> true
@ericnormand
ericnormand / 00 consecutive numbers.md
Created Dec 28, 2020
408 - PurelyFunctional.tv Newsletter
View 00 consecutive numbers.md

Consecutive numbers

Write a function that takes a string of digits. Try to break up the digits into consecutive integers. If you can, return them, otherwise, return nil.

Examples

(consec "121314") ;=> [12 13 14]
(consec "121315") ;=> nil
(consec "444445") ;=> [444 445]
@ericnormand
ericnormand / 00 overlapping rectangles.md
Created Dec 14, 2020
407 - PurelyFunctional.tv Newsletter
View 00 overlapping rectangles.md

Area of overlapping rectangles

Write a function that takes two rectangles and returns the area of the overlap. Sometimes the overlap is zero!

(overlap-area [{:top-left {:x 0 :y 0}
                :bottom-right {:x 10 :y 10}}
               {:top-left {:x 5 :y 5}
                :bottom-right {:x 15 :y 15}}]) ;=> 25
@ericnormand
ericnormand / 00 Design process.md
Last active Dec 19, 2020
406 - PurelyFunctional.tv Newsletter
View 00 Design process.md

Design process

This week's challenge is to describe your design process. How do you think before you build? What questions do you ask? What do you look for? Do you sketch, plan, and prototype?

Please submit your solutions as comments on this gist.

@ericnormand
ericnormand / 00 Sequence and parallel combinators.md
Created Nov 30, 2020
405 - PurelyFunctional.tv Newsletter
View 00 Sequence and parallel combinators.md

Sequence and parallel combinators

I've been re-reading the Lambda, the ultimate... papers again for my podcast. These papers show how functions and function calls can model many of the imperative constructs in programming languages. One practical application of that is to build a compiler whose main construct is the function call, which is a common way to implement Scheme.

In this challenge, we'd like to make constructs for two kinds of execution: sequential and parallel.

Imagine we had actions a and b, which are functions which depend on when they are called. If we want them to run in order, meaning a completes before b begins, we can create a new action a>b, like so:

(def a>b (sequential a b))
@ericnormand
ericnormand / 00 Boolean combinators.md
Created Nov 23, 2020
404 - PurelyFunctional.tv Newsletter
View 00 Boolean combinators.md

Boolean combinators

In the Clojure Tip above, I described a kind of Boolean combinator that lets us build complex rules out of simpler rules. Your task is to build those combinators. Please define:

(defn rule-and
  ([])
  ([rule])
@ericnormand
ericnormand / 00 Sort by content.md
Last active Nov 21, 2020
403 - PurelyFunctional.tv Newsletter
View 00 Sort by content.md

Sort by content

In this task, we are sorting a heterogeneous list of elements. The elements are either single numbers or sequences of numbers. We should sort them by numeric content, like this:

(sort-by-content [4 5 3 2 1]) ;=> [1 2 3 4 5]
;; we sort sequences by their first element, then by second element, then by third, etc
(sort-by-content [[2 3] [0 9] [-1 3 4] [0 3]]) ;=> [[-1 3 4] [0 3] [0 9] [2 3]]
;; we sort numbers and sequences together as if numbers were sequences of length 1
(sort-by-content [5 [4 5] 3 1 [0 2 3]]) ;=> [[0 2 3] 1 3 [4 5] 5]