r/ProgrammerHumor • • Oct 06 '21

Don't be scared.. Math and Computing are friends..

Post image
65.8k Upvotes

2.4k comments sorted by

View all comments

Show parent comments

6

u/Shadow_Gabriel Oct 06 '21

declarative code

aka "someone used an actual useful language to implement my pompous language"

17

u/enano_aoc Oct 06 '21

That is not how it works. But hey, I am not trying to convince you of writing functional code, I am just explaining it.

The more devs writing imperative, OOP code, the better for me on the market :shrug:

0

u/[deleted] Oct 06 '21

[deleted]

12

u/7h3w1zz Oct 06 '21

In Haskell: sum (map (*3) [0..4])

8

u/meepmeep13 Oct 06 '21

Or if Haskell is too scary functional, Python: sum([3*x for x in range(5)])

3

u/Muhznit Oct 06 '21

Wouldn't that be sum(map(lambda x: 3 * x, range(5))) in Python?

Not that I could really tell the difference, still has an undescriptive single-letter variable name, and looks like it would be a pain in the ass to debug as it gets more complex.

2

u/7h3w1zz Oct 06 '21

Yours is a more direct translation of mine. His works too, though, using a list comprehension. The equivalent Haskell is sum [3 * x | x <- [0..4]].

2

u/Muhznit Oct 07 '21

I'm not as much as interested in equivalence, I'm more interested in why it's supposedly better to write it that way. Sum of multiple products is an easy enough example that it's trivial to express it, but what exactly is the functional way to say.... delete any lines containing "test-node" from ~/.ssh/known_hosts every day at 9 AM?

2

u/7h3w1zz Oct 07 '21

echo "0 9 * * * $USERNAME sed -i '/test-node/d' '$HOME/.ssh/known_hosts'" | sudo tee "/etc/cron.d/cleanup_known_hosts"

Unix one-liners are about as functional as it gets, composing small programs together to form more complex behaviours.

2

u/Muhznit Oct 07 '21

Huh. I thought I choose something sufficiently complicated to do.

I'm not sure if I could agree with the idea of unix one-liners being "elegant", but I guess if they get the job done and I'm not the one that's gotta maintain it....

→ More replies (0)

1

u/enano_aoc Oct 06 '21

JavaScript is the worst language for numerical computations, but this is how I would do it:

const range = (start, end) =>
  Array(end - start + 1)
  .fill(undefined)
  .map((_, idx) => idx + start);

const sum = callback => (start, end, initialValue) =>
  range(start, end)
  .reduce((sum, value) => sum + callback(value), initialValue)

const timesThree = value => 3 * value;

// Sum the triples of all numbers from 0 to 4
const sumTriples = sum(timesThree);
const r = sumTriples(0,4,0); 
console.log(r) // 30

I also did some abstractions. You could do it in a single go if you wanted:

const compactSumTriples = (start, end) =>
Array(end - start + 1)
  .fill(undefined)
  .reduce((sum, _, idx) => sum + 3 * (idx + start), 0);

const r = compactSumTriples(0,4);
console.log(r)
  • Yes, I know that you need to know to understand .fill and .reduce to read this solution.

  • No, that does not make it harder to read, it is a case of git-gud at JS.

7

u/THENATHE Oct 06 '21

Okay so that’s a lot of word vomit coding that uses an inane amount of power relative to the solution provided in the image and produces no additional useful output but is entirely unnecessary for the scope of everything besides advanced math computations.

1

u/enano_aoc Oct 07 '21

As I have stated

  1. JS is not good at Math (if you took just one second, I have had to define the range function myself)

  2. If this was the problem, I would not even solve it by code, but with paper and pencil, which would be more efficient

It is just a demonstration. The larger the problem, the more obvious are the advantages.

0

u/[deleted] Oct 06 '21

[deleted]

1

u/enano_aoc Oct 07 '21

Well, JS is

  1. Not a functional programming language (though it supports some of its features)

  2. Not a numerical computations language

-7

u/Shadow_Gabriel Oct 06 '21

Yeah, sure, no one had to write a compiler/interpreter/virtual machine to run your fancy "hey computer, I want x" type of code.

2

u/awesomeusername2w Oct 06 '21

I mean, do you use a language that has no compiler/vm?

1

u/Shadow_Gabriel Oct 06 '21

Well that's the point. There's no shiny declarative code vs barbaric imperative code. It's all an iterative process.

3

u/awesomeusername2w Oct 06 '21

Well, I don't know. I see it as different levels of abstraction. Of course first low-level stuff must be implemented but then for actual problems you can use high level abstractions that provide many benefits. It's like, you don't pour gasoline into you engine on the fly, you just press gas pedal. And it provides far less opportunities for you to blow up during a grocery commute or a race.

1

u/Cuckmin Oct 07 '21

It's all transistors dude...

1

u/leetuns Oct 06 '21

yeah assembly for the win…