How difficult is writing a
for
loop?
We start programming by learning how to perform a loop, such as the following example.
for (let i = 0; i < 6; i++) { print i; }
I don’t know if this bothers you, but looping isn’t pretty to look at. Not to mention the additional boilerplate as well as keeping track of the extra index. Moreover it breaks the flow of your reasoning. Mentally you have to take a break whenever you encounter a loop.
It is costly, for sure, we don’t want to approach problem in higher dimensions, we tend to flatten our thoughts so that we can stay in a linear one dimensional structure most of time thus the visualization of the data flow becomes effortless. …
Is this level of trouble worth it in the end?
Like any tech-savvy person who would chase the latest computer spec, I’m very much into understanding what’s really going on with a new language, especially when the new language starts to show its potential in practical fields.
The learning isn’t that difficult, at least not the beginning. I’ve been practicing the functional programming, comfortable of functions as a first class citizens, and strongly feeling that function probably is what the programming is about, not the implementation.
>>> length (takeWhile (<1000) (scanl1 (+) (map sqrt [1..]))) + 1
With recursive feature and chaining (via $ and dot), one nice thing that
Haskell
can do is writing code with fewer lines. It’s not only short, but also concise and (easy) to understand, easy being subjective. …
After ES6
, we tend to use map
, filter
a lot. And sometimes if we really need to get hands dirty, we can use reduce
where we take an initial state and reduce it to next state going through all elements.
Looping through the entire array can be a bit expensive, especially when you want to stop when certain condition is met. This conditional reduce can be implemented using a Lazy library, where each element traverse is deferred till needed, therefore only loops to the point when a match is found. …
ES6
adds quite a bit functionalities to array with map
, filter
etc. They are extremely handy, but I start to have trouble keeping track of the indexes.
For instance
arr.filter(v => v == 3)
We can find out if there’s a specific element that we need, however once it’s filtered, we lost its original index. Therefore we tend to use indexOf
. But the point here is that we do want to filter, at the same time we need to have additional properties to be preserved during the process.
In order to do that, we could do
arr
.map((v, i) => ({ v, i }))
.filter(t => t.v …
Create an array in Javascript isn’t 100% straightforward as in other languages. Although on the surface it does seem simple, and most of the online tutorial can put you in the right (working) direction. Does it occur to you it’s not well designed?
Consider the following array statement, what’s odd is that
const a = new Array(10)
map
right awayfill
There’re many libraries (even ES6
) extending the array functionalities, but they rarely talk about the definition or the initialization of the array. …
Question: Advent of Code 2015, Day 3
<^^<>v^v^vv<>>>>v^v<>><^^v>vv^^>v^v>v<vv>>v>><
vv>>^>^^<>><>^>vvv>>^vv>^<><>^<v^>^>^><vv^vv^>
v^^vv><<<>v<>v>^<vvv^<^<v<v<^vv^^>>vv^<^^v^><^
^^^^v<^<v<^>>>vv^v^>^<v>^<><v^<^v>>><^v^<<v<<v
<>v>^v<v^v>>^^v<<v<v<<>>>vv>>^v>>^<<<<^><<<><^
import Data.List (nub)type Pos = (a, a)next :: (Num a) => Pos -> Char -> Pos
next (x,y) '^' = (x, y+1)
next (x,y) 'v' = (x, y-1)
next (x,y) '<' = (x-1, y)
next (x,y) '>' = (x+1, y)
next (x,y) _ = (x,y)visited :: Num a => [Char] -> [Pos]
visited = scanl next (0,0)uniqueVisits :: Eq a => [a] -> Int
uniqueVisits = length . nub-- Nefrubyr's every from StacksOverflow
every :: Int -> [a] -> [a]
every n xs = case drop (n-1) xs of
(y:ys) -> y : every n ys
[] ->…
Question: Advent of Code 2015, Day 2
3x11x24
13x5x19
1x9x27
24x8x21
6x8x17
Given a box as string 2x2x2
, we can split it as measurements that can be applied to wrapping paper calculation.
type Box = [Char]split :: Box -> [Int]
split "" = []
split s = fp : rest
where parts = break (=='x') s
fp = read (fst parts)::Int
rest = split $ drop 1 $ snd partswrapBox :: [Int] -> Int
wrapBox b@(l:w:h:[]) = area + minimum b
where area = sum d * 2
d = zipWith (*) [l, w, h] [w, h, l]
part1 :: (Foldable t, Functor t) => t Box -> Int
part1 ls = sum $ fmap (wrapBox . …
The question can be viewed from Advent of Code 2015, Day 1
()()(()()()(()()((()((()))((()((((()()((((((
)()(()()()(()()((()((()))((()((((()()((((())
)()((((())(((((((()(((((((((()(((())(()()(()
((()()(()(())(()((((()((()()()((((())(((((((
)(()(((()())(()((((()))())(())(()(()()))))))
Assign each char with a number, ex. `-1` or `1`…
When I am working for Advent of Code
competition, I find that I always need to write some utility function for Javascript
to simplify the workflow. This is especially true after I learned a few tricks from Ruby
and Kotlin
. And I realize that the language does not matter too much for simple problems. What matters more is the way of drafting the workflow and writing functions.
import _ from 'lodash'
I used lodash
as my baseline, but you can replace it with any other similar libraries. …
From Advent of Code 2015, Day 3
v>v<vvv<<vv^v<v>vv>v<<<^^^^^<<^<vv>^>
v^>^>^>^>^><vvvv<^>^<<^><<<^vvvv>^>^>
<^v^><^<>^^>^vvv^<vv>>^>^^<>><>^>vvv>
>^vv>^<><>^<v^>^>^><vv^vv^>><<^><<v>>
<>^<^>>vvv>v>>>v<<^<><^<v<>v>^^v^^^<^
v^^>>><^>^>v<>^<>>^>^^v^><v<v>>><>v<v
We model a position object as a simple Int
.
Given an instruction char
and current position acc
, we can figure out the next position based on the direction it takes.
We lower the dimension to use 1-D coordinates for 2-D layout, hoping it produces quicker coding and simpler understanding for the problem.
Given a list of direction instructions, find out total number of places visited.
We scan
all the instructions to get snapshots of future positions, among them we can find the distinct
set and the count
. …
About