diff --git a/README.md b/README.md index 9f06617..c23bb41 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_391 TILs and counting..._ +_392 TILs and counting..._ --- @@ -43,6 +43,7 @@ _391 TILs and counting..._ - [Aggregation Using merge-with](clojure/aggregation-using-merge-with.md) - [Argument Requirements For A Function](clojure/argument-requirements-for-a-function.md) +- [Combinations Of Items From A Sequence](clojure/combinations-of-items-from-a-sequence.md) - [Evaluate One Liners With lein-exec](clojure/evaluate-one-liners-with-lein-exec.md) - [Expanding Macros](clojure/expanding-macros.md) - [Get The Value Of An Environment Variable](clojure/get-the-value-of-an-environment-variable.md) diff --git a/clojure/combinations-of-items-from-a-sequence.md b/clojure/combinations-of-items-from-a-sequence.md new file mode 100644 index 0000000..412e995 --- /dev/null +++ b/clojure/combinations-of-items-from-a-sequence.md @@ -0,0 +1,24 @@ +# Combinations Of Items From A Sequence + +Sometimes we want all combinations of items from a list. For instance, we +may have 5 people and we want to know all the ways that we can unique pair +up 2 people from that group of 5. What we want is the number of +combinations of 2 people from the 5. + +The +[clojure/math.combinatorics](https://github.com/clojure/math.combinatorics) +library provides a `combinations` function that gives us exactly that +functionality. + +```clojure +(use '[clojure.math.combinatorics :as combo]) + +(combo/combinations ["Liz", "Tracy", "Kenneth", "Jack", "Jenna"] 2) +; (("Liz" "Tracy") ("Liz" "Kenneth") ("Liz" "Jack") +; ("Liz" "Jenna") ("Tracy" "Kenneth") ("Tracy" "Jack") +; ("Tracy" "Jenna") ("Kenneth" "Jack") ("Kenneth" "Jenna") +; ("Jack" "Jenna")) +``` + +The `combinations` function takes a list of items and then a number for the +size of the grouping combinations that it is supposed to produce.