tips
during my ramblings, i comment on the solutions of others
sometimes i pick at things, rather it be something that i see as more busy than it needs to be, or a solution that looks like its not making use of a language feature / library / syntax that it could to be cleaner
instead of pointing out solutions to everything, this tips page will be used as a point of reference of how to do things a bit cleaner, and but most importantly in my opinion, therefore please take what i say with a grain of salt, but if you do respect and apply my recommendations, nice
table of contents:
general
functional programming
use the functions, luke!
this is the one to take the most grains of salt with
i am solving my problems with [Elixir] this year, which is a functional programming language, so my headspace while solving the problems myself is very functional, therefore suprisingly when i look at other peoples code, i will probably notice when it is / isn’t functional
a great example of this was commenting on [andrew reynolds]
day 3 solution, that it was so close to being functional, and recommended refactoring his forEach
a reduce
, since it was being used to create a sum
now andrew did go and do this the next day, which is flattering,
but mentioned that it ate up the ‘majority’ of his time refactoring
a forEach
into a reduce
its not my goal here to affect peoples problem solving strategies in such a way that things get harder, only to point out my comment / opinions on things
(while its not my goal, if it happens, and the person enjoys my methodology, i won’t complain .-. )
i’ve gotten ahead of myself and not even defined functional programming, oops
from [wikipedia]:
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that map values to other values, rather than a sequence of imperative statements which update the running state of the program.
language-specific
python
list comprehension
with list comprehension
squares = []
for x in range(10):
squares.append(x**2)
this
squares = [x**2 for x in range(10)]
can become this
list comprehension is a neat tool to be aware of, and use to iterate over things without the use of a full-size for loop, in one small line
the [official python docs] cover them well
but please don’t go crazy with these
i usually find reading other peoples list comprehension statements confusing, and that might just be a personal preference thing, but i think there is potentially a sacrifice of readability over space
if your goal is to [code golf] and get your solution as small as possible, 100% abuse these to the max
to pluck some [day 3 code from mudkip]
rucksacks = ((set(list(x[:len(x)//2])), set(list(x[len(x)//2:]))) for x in input)
there is too much going on here!
now, for mudkip, the goal is to have lots going on in one line, i.e. code golfing
but if i see this sort of thing in code at my workplace, i screech
but, from [brianna’s solution to day4]
r1x, r1y = [int(x) for x in range1.split("-")]
this is a nice, but more importantly in my opinion, simple usage of list comprehension
this is still doing a lot! its splitting some string on "-"
, mapping each value
returned from this to an interger, and unpacking the returned list into two values
typescript
deno
the land of the dinosaurs
might not be following a good tutorial / the correct path, but i find quickly bootstrappping
a sensible typescript node project frustrating, usually i reach for ts-node
, but i always
run into several different issues, and have to play wack-a-mole with them to make it behave /
not give me errors / actually run the code
if this is the case, you might want to give [Deno] a go instead
even if its not to have a better typescript expierence, [check out the deno standard library], which is packed with useful modules, such as [collections]
Functions for specific common tasks around collection types like Array and Record. This module is heavily inspired by kotlins stdlib.
with functions / classes such as
-
intersect
: Returns all distinct elements that appear at least once in each of the given arrays. -
BinarySearchTree
: An unbalanced binary search tree. The values are in ascending order by default, using JavaScript’s built-in comparison operators to sort the values.
and look how easy / simple it is to do basic things like [read a file] without a bunch of fluff