Other Clojure solutions. (ns raindrops)
(defn- substitute-factors-with [n substitutions]
(let [factors (sort (keys substitutions))
converted (map
(fn [factor]
(if (= 0 (mod n factor))
(get substitutions factor)
""))
factors)
s (apply str converted)]
(if (empty? s)
(str n)
s)))
(defn convert [n]
(substitute-factors-with n {3 "Pling"
5 "Plang"
7 "Plong"}))
Other Elm solutions. module Raindrops exposing (raindrops)
raindrops : Int -> String
raindrops number =
let
labelledRaindrops =
labelFactors number
[ ( 3, "Pling" )
, ( 5, "Plang" )
, ( 7, "Plong" )
]
in
stringWithDefault (String.fromInt number) labelledRaindrops
labelFactors : Int -> List ( Int, String ) -> String
labelFactors number =
List.filterMap (labelFactor number) >> String.concat
labelFactor : Int -> ( Int, String ) -> Maybe String
labelFactor num ( factor, label ) =
if modBy factor num == 0 then
Just label
else
Nothing
-- String.Extra
{-| It would be nice if we had String.withDefault,
The behaviour is the same as Maybe.withDefault, except it uses the default value in the case of the empty string
stringWithDefault "It looks like we have no fruit :(" (String.join ", " fruit)
-}
stringWithDefault : String -> String -> String
stringWithDefault default value =
Maybe.withDefault default (nonEmpty value)
nonEmpty : String -> Maybe String
nonEmpty string =
if String.isEmpty string then
Nothing
else
Just string
Other Python solutions. def convert(number):
result = ""
if (number % 3) == 0:
result += "Pling"
if (number % 5) == 0:
result += "Plang"
if (number % 7) == 0:
result += "Plong"
if result == "":
result = str(number)
return result
Other Roc solutions. module [convert]
convert : U64 -> Str
convert = \number ->
pling = if number % 3 == 0 then "Pling" else ""
plang = if number % 5 == 0 then "Plang" else ""
plong = if number % 7 == 0 then "Plong" else ""
result = Str.joinWith [pling, plang, plong] ""
if result == "" then
Num.toStr number
else
result
Other Rust solutions. pub fn raindrops(n: u32) -> String {
let rain = [(3, "Pling"), (5, "Plang"), (7, "Plong")]
.into_iter()
.filter(|&(number, _)| n % number == 0)
.map(|&(_, sound)| sound)
.collect::<Vec<&str>>()
.join("");
return if rain.is_empty() { n.to_string() } else { rain };
}