Pascal's Triangle - Remove hack
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I had a thought last night on how I could easily write a Pascal's Triangle generator basically just using partition
and map
. I tried writing it this morning, and it got slightly more complicated than I originally thought it would, although it's still pretty nice.
My main concern is the wrap
"hack" that I'm using. I found two similar, but different ways of writing it. I can either wrap the input to the iteration function in 0s, or I can wrap the result of the iteration function in 1s. Both give the same result, but the need for a wrapping helper pollutes an otherwise nice ->>
chain. If anyone can think of how I can avoid a separate anonymous function here, and ideally avoid wrapping the input/output in 0s/1s, I'd appreciate it. I'll take any other critique as well, although there isn't much else going on here.
Usage:
(let [pre-t (pascals-triangle-pre) ; Wrapping pre-processing
post-t (pascals-triangle-post) ; Wrapping post-processing
n 5]
(println (take n pre-t))
(println (take n post-t)))
([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
(ns minesweeper.irrelevant.pas-tri)
(defn pascals-triangle-post
(let [wrap #(vec (concat [1] % [1]))]
(iterate #(->> %
(partition 2 1)
(mapv (partial apply +'))
(wrap))
[1])))
(defn pascals-triangle-pre
(let [wrap #(vec (concat [0] % [0]))]
(iterate #(->> %
(wrap)
(partition 2 1)
(mapv (partial apply +')))
[1])))
clojure
add a comment |Â
up vote
2
down vote
favorite
I had a thought last night on how I could easily write a Pascal's Triangle generator basically just using partition
and map
. I tried writing it this morning, and it got slightly more complicated than I originally thought it would, although it's still pretty nice.
My main concern is the wrap
"hack" that I'm using. I found two similar, but different ways of writing it. I can either wrap the input to the iteration function in 0s, or I can wrap the result of the iteration function in 1s. Both give the same result, but the need for a wrapping helper pollutes an otherwise nice ->>
chain. If anyone can think of how I can avoid a separate anonymous function here, and ideally avoid wrapping the input/output in 0s/1s, I'd appreciate it. I'll take any other critique as well, although there isn't much else going on here.
Usage:
(let [pre-t (pascals-triangle-pre) ; Wrapping pre-processing
post-t (pascals-triangle-post) ; Wrapping post-processing
n 5]
(println (take n pre-t))
(println (take n post-t)))
([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
(ns minesweeper.irrelevant.pas-tri)
(defn pascals-triangle-post
(let [wrap #(vec (concat [1] % [1]))]
(iterate #(->> %
(partition 2 1)
(mapv (partial apply +'))
(wrap))
[1])))
(defn pascals-triangle-pre
(let [wrap #(vec (concat [0] % [0]))]
(iterate #(->> %
(wrap)
(partition 2 1)
(mapv (partial apply +')))
[1])))
clojure
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I had a thought last night on how I could easily write a Pascal's Triangle generator basically just using partition
and map
. I tried writing it this morning, and it got slightly more complicated than I originally thought it would, although it's still pretty nice.
My main concern is the wrap
"hack" that I'm using. I found two similar, but different ways of writing it. I can either wrap the input to the iteration function in 0s, or I can wrap the result of the iteration function in 1s. Both give the same result, but the need for a wrapping helper pollutes an otherwise nice ->>
chain. If anyone can think of how I can avoid a separate anonymous function here, and ideally avoid wrapping the input/output in 0s/1s, I'd appreciate it. I'll take any other critique as well, although there isn't much else going on here.
Usage:
(let [pre-t (pascals-triangle-pre) ; Wrapping pre-processing
post-t (pascals-triangle-post) ; Wrapping post-processing
n 5]
(println (take n pre-t))
(println (take n post-t)))
([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
(ns minesweeper.irrelevant.pas-tri)
(defn pascals-triangle-post
(let [wrap #(vec (concat [1] % [1]))]
(iterate #(->> %
(partition 2 1)
(mapv (partial apply +'))
(wrap))
[1])))
(defn pascals-triangle-pre
(let [wrap #(vec (concat [0] % [0]))]
(iterate #(->> %
(wrap)
(partition 2 1)
(mapv (partial apply +')))
[1])))
clojure
I had a thought last night on how I could easily write a Pascal's Triangle generator basically just using partition
and map
. I tried writing it this morning, and it got slightly more complicated than I originally thought it would, although it's still pretty nice.
My main concern is the wrap
"hack" that I'm using. I found two similar, but different ways of writing it. I can either wrap the input to the iteration function in 0s, or I can wrap the result of the iteration function in 1s. Both give the same result, but the need for a wrapping helper pollutes an otherwise nice ->>
chain. If anyone can think of how I can avoid a separate anonymous function here, and ideally avoid wrapping the input/output in 0s/1s, I'd appreciate it. I'll take any other critique as well, although there isn't much else going on here.
Usage:
(let [pre-t (pascals-triangle-pre) ; Wrapping pre-processing
post-t (pascals-triangle-post) ; Wrapping post-processing
n 5]
(println (take n pre-t))
(println (take n post-t)))
([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
([1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1])
(ns minesweeper.irrelevant.pas-tri)
(defn pascals-triangle-post
(let [wrap #(vec (concat [1] % [1]))]
(iterate #(->> %
(partition 2 1)
(mapv (partial apply +'))
(wrap))
[1])))
(defn pascals-triangle-pre
(let [wrap #(vec (concat [0] % [0]))]
(iterate #(->> %
(wrap)
(partition 2 1)
(mapv (partial apply +')))
[1])))
clojure
asked May 14 at 16:14
Carcigenicate
2,31911128
2,31911128
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194374%2fpascals-triangle-remove-hack%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password