Skip to main content
deleted 24 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26

Some notes about your code:

  • (function () {. Is this old-style wrapping really needed? Isn't this a node.js script?
  • You parsed INPUT manually from the question. You should probably leave the input as similar as possible to how it's stated and do the parsing programatically.
  • const multiply = (acc, val) => acc * val;: The variable names reveal how the function is used; instead, keep it generic: const multiply = (x, y) => x * y;. Consider also creating the abstraction product, the product of all numbers in an array.
  • parser(latestMaxProduct, number.slice(1)); You should use recursive calls as a last resort, only when higher-level abstractions (map, filter, reduce, ...) are not enough. Even in this case you may consider creating a generic (recursive) abstraction that works for you.
  • I'd separate generic functions -which may be used in other problems- from the specific code used in the problem. Re-using generic abstractions is one the fundamental principles of FP.

I'd write:

const range = (start, end) => Array.from(new Array(end - start), (x, i) => i + start); const groupsOf = (xs, n) => range(0, xs.length - n + 1).map(i => xs.slice(i, i + n)); const product = xs => xs.reduce((acc, x) => acc * x, 1); const maximum = xs => xs.reduce((acc, x) => Math.max(acc, x)...xs); const euler8 = () => { const unparsedDigits = ` 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 ... `; const groupSize = 13; const digits = unparsedDigits.replace(/[^\d]/g, "").split("").map(c => parseInt(c)); return maximum(groupsOf(digits, groupSize).map(product)); } console.log(euler8()); 

Some notes about your code:

  • (function () {. Is this old-style wrapping really needed? Isn't this a node.js script?
  • You parsed INPUT manually from the question. You should probably leave the input as similar as possible to how it's stated and do the parsing programatically.
  • const multiply = (acc, val) => acc * val;: The variable names reveal how the function is used; instead, keep it generic: const multiply = (x, y) => x * y;. Consider also creating the abstraction product, the product of all numbers in an array.
  • parser(latestMaxProduct, number.slice(1)); You should use recursive calls as a last resort, only when higher-level abstractions (map, filter, reduce, ...) are not enough. Even in this case you may consider creating a generic (recursive) abstraction that works for you.
  • I'd separate generic functions -which may be used in other problems- from the specific code used in the problem. Re-using generic abstractions is one the fundamental principles of FP.

I'd write:

const range = (start, end) => Array.from(new Array(end - start), (x, i) => i + start); const groupsOf = (xs, n) => range(0, xs.length - n + 1).map(i => xs.slice(i, i + n)); const product = xs => xs.reduce((acc, x) => acc * x, 1); const maximum = xs => xs.reduce((acc, x) => Math.max(acc, x)); const euler8 = () => { const unparsedDigits = ` 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 ... `; const groupSize = 13; const digits = unparsedDigits.replace(/[^\d]/g, "").split("").map(c => parseInt(c)); return maximum(groupsOf(digits, groupSize).map(product)); } console.log(euler8()); 

Some notes about your code:

  • (function () {. Is this old-style wrapping really needed? Isn't this a node.js script?
  • You parsed INPUT manually from the question. You should probably leave the input as similar as possible to how it's stated and do the parsing programatically.
  • const multiply = (acc, val) => acc * val;: The variable names reveal how the function is used; instead, keep it generic: const multiply = (x, y) => x * y;. Consider also creating the abstraction product, the product of all numbers in an array.
  • parser(latestMaxProduct, number.slice(1)); You should use recursive calls as a last resort, only when higher-level abstractions (map, filter, reduce, ...) are not enough.
  • I'd separate generic functions from the specific code used in the problem. Re-using generic abstractions is one the fundamental principles of FP.

I'd write:

const range = (start, end) => Array.from(new Array(end - start), (x, i) => i + start); const groupsOf = (xs, n) => range(0, xs.length - n + 1).map(i => xs.slice(i, i + n)); const product = xs => xs.reduce((acc, x) => acc * x, 1); const maximum = xs => Math.max(...xs); const euler8 = () => { const unparsedDigits = ` 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 ... `; const groupSize = 13; const digits = unparsedDigits.replace(/[^\d]/g, "").split("").map(c => parseInt(c)); return maximum(groupsOf(digits, groupSize).map(product)); } console.log(euler8()); 
deleted 16 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26

Some notes about your code:

  • (function () {. Is this old-style wrapping really needed? Isn't this a node.js script?
  • You parsed INPUT manually from the question. You should probably leave the input as similar as possible to how it's stated and do the parsing programatically.
  • const multiply = (acc, val) => acc * val;: The variable names reveal how the function is used; instead, keep it generic: const multiply = (x, y) => x * y;. Consider also creating the abstraction product, the product of all numbers in an array.
  • parser(latestMaxProduct, number.slice(1)); You should use recursive calls as a last resort, only when higher-level abstractions (map, filter, reduce, ...) are not enough. Even in this case you may consider creating a generic (recursive) abstraction that works for you.
  • I'd separate generic functions -which may be used in other problems- from the specific code used in the problem. Re-using generic abstractions is one the fundamental principles of FP.

I'd write:

const range = (start, end) => Array.from(new Array(end - start), (x, i) => i + start); const groupsOf = (xs, n) => range(0, xs.length - n + 1).map(i => xs.slice(i, i + n)); const product = xs => xs.reduce((acc, x) => acc * x, 1); const maximum = xs => xs.reduce((acc, x) => Math.max(acc, x)); const euler8 = () => { const unparsedDigits = ` 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 ... `; const groupSize = 13; const digitsLinesdigits = unparsedDigits.trim().splitreplace(/\n[^\d]/).map(s => s.trim()); const digits =g, digitsLines.join("").split("").map(c => parseInt(c)); return maximum(groupsOf(digits, groupSize).map(group => product(group))); } console.log(euler8()); 

Some notes about your code:

  • (function () {. Is this old-style wrapping really needed? Isn't this a node.js script?
  • You parsed INPUT manually from the question. You should probably leave the input as similar as possible to how it's stated and do the parsing programatically.
  • const multiply = (acc, val) => acc * val;: The variable names reveal how the function is used; instead, keep it generic: const multiply = (x, y) => x * y;. Consider also creating the abstraction product, the product of all numbers in an array.
  • parser(latestMaxProduct, number.slice(1)); You should use recursive calls as a last resort, only when higher-level abstractions (map, filter, reduce, ...) are not enough. Even in this case you may consider creating a generic (recursive) abstraction that works for you.
  • I'd separate generic functions -which may be used in other problems- from the specific code used in the problem. Re-using generic abstractions is one the fundamental principles of FP.

I'd write:

const range = (start, end) => Array.from(new Array(end - start), (x, i) => i + start); const groupsOf = (xs, n) => range(0, xs.length - n + 1).map(i => xs.slice(i, i + n)); const product = xs => xs.reduce((acc, x) => acc * x, 1); const maximum = xs => xs.reduce((acc, x) => Math.max(acc, x)); const euler8 = () => { const unparsedDigits = ` 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 ... `; const groupSize = 13; const digitsLines = unparsedDigits.trim().split(/\n/).map(s => s.trim()); const digits = digitsLines.join("").split("").map(c => parseInt(c)); return maximum(groupsOf(digits, groupSize).map(group => product(group))); } console.log(euler8()); 

Some notes about your code:

  • (function () {. Is this old-style wrapping really needed? Isn't this a node.js script?
  • You parsed INPUT manually from the question. You should probably leave the input as similar as possible to how it's stated and do the parsing programatically.
  • const multiply = (acc, val) => acc * val;: The variable names reveal how the function is used; instead, keep it generic: const multiply = (x, y) => x * y;. Consider also creating the abstraction product, the product of all numbers in an array.
  • parser(latestMaxProduct, number.slice(1)); You should use recursive calls as a last resort, only when higher-level abstractions (map, filter, reduce, ...) are not enough. Even in this case you may consider creating a generic (recursive) abstraction that works for you.
  • I'd separate generic functions -which may be used in other problems- from the specific code used in the problem. Re-using generic abstractions is one the fundamental principles of FP.

I'd write:

const range = (start, end) => Array.from(new Array(end - start), (x, i) => i + start); const groupsOf = (xs, n) => range(0, xs.length - n + 1).map(i => xs.slice(i, i + n)); const product = xs => xs.reduce((acc, x) => acc * x, 1); const maximum = xs => xs.reduce((acc, x) => Math.max(acc, x)); const euler8 = () => { const unparsedDigits = ` 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 ... `; const groupSize = 13; const digits = unparsedDigits.replace(/[^\d]/g, "").split("").map(c => parseInt(c)); return maximum(groupsOf(digits, groupSize).map(product)); } console.log(euler8()); 
added 97 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26

Some notes about your code:

  • (function () {. Is this old-style wrapping really needed? Isn't this a node.js script?
  • You parsed INPUT manually from the question. You should probably leave the input as similar as possible to how it's stated and do the parsing programatically.
  • const multiply = (acc, val) => acc * val;: The variable names reveal how the function is used; instead, keep it generic: const multiply = (x, y) => x * y;. Consider also creating the abstraction product, the product of all numbers in an array.
  • parser(latestMaxProduct, number.slice(1)); You should use recursive calls as a last resort, only when higher-level abstractions (map, filter, reduce, ...) are not enough. Even in this case you may consider creating a generic (recursive) abstraction that works for you.
  • I'd separate generic functions -which may be used in other problems- from the specific code used in the problem. Re-using generic abstractions is one the fundamental principles of FP.

I'd write:

const range = (start, end) => Array.from(new Array(end - start), (x, i) => i + start); const groupsOf = (xs, n) => range(0, xs.length - n + 1).map(i => xs.slice(i, i + n)); const product = xs => xs.reduce((acc, x) => acc * x, 1); const maximum = xs => xs.reduce((acc, x) => Math.max(acc, x)); const euler8 = () => { const unparsedDigits = ` 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 ... `; const groupSize = 13; const digitsLines = unparsedDigits.trim().split(/\n/).map(s => s.trim()); const digits = digitsLines.join("").split("").map(c => parseInt(c)); return maximum(groupsOf(digits, groupSize).map(group => product(group))); } console.log(euler8()); 

Some notes about your code:

  • (function () {. Is this old-style wrapping really needed? Isn't this a node.js script?
  • You parsed INPUT manually from the question. You should probably leave the input as similar as possible to how it's stated and do the parsing programatically.
  • const multiply = (acc, val) => acc * val;: The variable names reveal how the function is used; instead, keep it generic: const multiply = (x, y) => x * y;. Consider also creating the abstraction product, the product of all numbers in an array.
  • parser(latestMaxProduct, number.slice(1)); You should use recursive calls as a last resort, only when higher-level abstractions (map, filter, reduce, ...) are not enough.
  • I'd separate generic functions -which may be used in other problems- from the specific code used in the problem. Re-using generic abstractions is one the fundamental principles of FP.

I'd write:

const range = (start, end) => Array.from(new Array(end - start), (x, i) => i + start); const groupsOf = (xs, n) => range(0, xs.length - n + 1).map(i => xs.slice(i, i + n)); const product = xs => xs.reduce((acc, x) => acc * x, 1); const maximum = xs => xs.reduce((acc, x) => Math.max(acc, x)); const euler8 = () => { const unparsedDigits = ` 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 ... `; const groupSize = 13; const digitsLines = unparsedDigits.trim().split(/\n/).map(s => s.trim()); const digits = digitsLines.join("").split("").map(c => parseInt(c)); return maximum(groupsOf(digits, groupSize).map(group => product(group))); } console.log(euler8()); 

Some notes about your code:

  • (function () {. Is this old-style wrapping really needed? Isn't this a node.js script?
  • You parsed INPUT manually from the question. You should probably leave the input as similar as possible to how it's stated and do the parsing programatically.
  • const multiply = (acc, val) => acc * val;: The variable names reveal how the function is used; instead, keep it generic: const multiply = (x, y) => x * y;. Consider also creating the abstraction product, the product of all numbers in an array.
  • parser(latestMaxProduct, number.slice(1)); You should use recursive calls as a last resort, only when higher-level abstractions (map, filter, reduce, ...) are not enough. Even in this case you may consider creating a generic (recursive) abstraction that works for you.
  • I'd separate generic functions -which may be used in other problems- from the specific code used in the problem. Re-using generic abstractions is one the fundamental principles of FP.

I'd write:

const range = (start, end) => Array.from(new Array(end - start), (x, i) => i + start); const groupsOf = (xs, n) => range(0, xs.length - n + 1).map(i => xs.slice(i, i + n)); const product = xs => xs.reduce((acc, x) => acc * x, 1); const maximum = xs => xs.reduce((acc, x) => Math.max(acc, x)); const euler8 = () => { const unparsedDigits = ` 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 ... `; const groupSize = 13; const digitsLines = unparsedDigits.trim().split(/\n/).map(s => s.trim()); const digits = digitsLines.join("").split("").map(c => parseInt(c)); return maximum(groupsOf(digits, groupSize).map(group => product(group))); } console.log(euler8()); 
added 1 character in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
added 48 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
added 1 character in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
deleted 4 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
deleted 1067 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading