001 (ns dog-and-duck.quack.picky.distribution)
002
003 ;;; Copyright (C) Simon Brooke, 2022
004
005 ;;; This program is free software; you can redistribute it and/or
006 ;;; modify it under the terms of the GNU General Public License
007 ;;; as published by the Free Software Foundation; either version 2
008 ;;; of the License, or (at your option) any later version.
009
010 ;;; This program is distributed in the hope that it will be useful,
011 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
012 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 ;;; GNU General Public License for more details.
014
015 ;;; You should have received a copy of the GNU General Public License
016 ;;; along with this program; if not, write to the Free Software
017 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
018
019 (defn distribution
020 "Distribution of values of function `f` when applied to `vals`.
021
022 I *know* there's a library function that does this, probably better, but I
023 don't remember what it's called!"
024 [f vals]
025 (loop [result {} values vals]
026 (if (empty? values) result
027 (let [r (apply f (list (first values)))]
028 (recur
029 (assoc result r (if (result r) (inc (result r)) 1))
030 (rest values))))))