)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Golfed Codepen – 3D Spiral Thing

  1. // sort of golfed version of https://www.instagram.com/p/C1uv6Kqv19T/
  2. // by @mewtru
  3. b = document.body
  4. a = Object.assign
  5. a(b.style, { background:'#000', color:'#fff'})
  6. w = 'snippet.zone snippet.zone'.toUpperCase().split``
  7. F = (n, O = 0, C, S, o, t) => { 
  8.   b.innerHTML += `<div id=${n} style='position:absolute;left:50%;top:50%;translate:-50% -50%;width:100% text-align:center;white-space:nowrap'></div>`
  9.   w.map(l => this[n].innerHTML += `<span style='display:inline-block;margin-right:5px;font-size:28px'>${l}</span>`)
  10.   t = O
  11.   setInterval(_ => {
  12.     t += .005
  13.     ;[...this[n].children].map((e, i) => { 
  14.       T = t + i / 2.7
  15.       a(e.style, {
  16.         translate: `0 ${Math.sin(T) * 100}px`,
  17.         scale: Math.cos(T) * .5 + .5})
  18.     }, 16)
  19.   })
  20. }
  21. F('Z') 
  22. F('X', 3)

“Very cool” pen by Lucas Fernando that comes from @mewtru
I decided to do a speed-coded semi-golfed version… can definitely be way more golfed 😀

Em3w parensordot

  1. const vals = {
  2.   E: 0, M: 90, '3': 180, W: 270
  3. }
  4.  
  5. // const byDeg = {
  6. //   0: 'E', 90: 'M', 180: '3', 270: 'W'
  7. // }
  8. const byDeg = Object.keys(vals).reduce((acc, key) => { 
  9.   acc[vals[key]] = key;
  10.   return acc;
  11. }, {});
  12.  
  13.  
  14. const ops = {
  15.   ')': 90, '(' : -90, '|': 180
  16. }
  17.  
  18. function rotToSym(v) {
  19.   if (v < 0) v += 360;
  20.   const deg = Math.abs(v % 360);
  21.   return byDeg[deg]
  22. }
  23.  
  24. function op(char, op) {
  25.   if (char === 'M' && op === '|') return 'M'
  26.   if (char === 'W' && op === '|') return 'W'
  27.   let v = vals[char]
  28.   v += ops[op]
  29.   return rotToSym(v)
  30. }
  31.  
  32.  
  33. function emw(prog) {
  34.   prog = [...prog]
  35.  
  36.   let chars = []
  37.   let res = []
  38.   for (let i = 0; i < prog.length; i++) {
  39.     let char = prog[i]
  40.     if (vals[char] != null) {
  41.       chars.push(char)
  42.     } else if (ops[char]) {
  43.       chars = chars.map(v => op(v, char))
  44.     } else if (char === '.' && chars.length > 0) {
  45.       const num = chars.map(v => vals[v])
  46.         .reduce((a, b) => a + b, 0);
  47.       chars = [rotToSym(num)]
  48.     }
  49.   }
  50.   return chars.join``
  51. }
  52.  
  53. function expr(prog) {
  54.   const orig = prog;
  55.   prog = prog.split(/\s/)
  56.   return orig + ' = ' + prog.map(emw).join` `
  57. }
  58.  
  59. document.body.appendChild(
  60.   Object.assign(
  61.     document.createElement`pre`, {
  62.       innerHTML: 
  63. `
  64. Em3w parensordot 
  65.  
  66. // rotate 90 clockwise
  67. ${expr('EEE)')}
  68.  
  69. // rotate -90 clockwise
  70. ${expr('W(')}
  71. ${expr('W((')}
  72.  
  73. // mirror
  74. ${expr('E|')}
  75.  
  76. // mirror
  77. ${expr('EM|')}
  78.  
  79. ${expr('WW3) MM) EEEM).')}
  80.  
  81. // sum rotations
  82. ${expr('MMM.')}
  83.  
  84. ${expr(
  85. `E) E( E)) EEE))) E)))) MM33).`)}
  86. `}
  87.   )
  88. )

Fun thing I wanted to make for awhile… definitely going to do some doodles of this in my sketchbook…

Code description from GPT4 because I’m too lazy to write one…

This code is implementing a form of symbolic computation using a transformational language. This language has four primary symbols: “E”, “M”, “3”, and “W”. Each of these symbols is associated with a specific angle: 0, 90, 180, and 270 degrees, respectively.

There are also special symbols, which act as operators that perform certain transformations on the primary symbols:

  • “)”: Represents a rotation 90 degrees clockwise.
  • “(“: Represents a rotation 90 degrees counter-clockwise.
  • “|”: Represents a mirror operation, which reverses the direction. However, “M” and “W” are unaffected by this operation.
  • “.”: This operator sums up all previous primary symbols (by their respective degree values), resulting in a new symbol. The degree value of the new symbol is the sum of the previous degrees modulo 360.

The function emw(prog) interprets a string prog as a sequence of symbols and operations. It reads each character in the string and based on whether it’s a primary symbol, operator, or a “.”, it either adds the symbol to an array or performs an operation on the existing symbols in the array.

The expr(prog) function splits the input string prog into space-separated substrings. It interprets each substring as a sequence in the transformational language, using the emw function. It then joins the results together with a space and prepends the original input and an equals sign to give a string representation of the transformations.

The final section of the code generates a series of examples of expressions in this language, evaluates them using the expr function, and adds the results to the HTML body of a webpage in a formatted manner. This gives the user a way to see the transformations in action.

Validate Anagram

  1. function normalizeString(word) {
  2.   word = word.toLowerCase()
  3.   const result = normalizeString.memo[word]
  4.   if (result) return result
  5.   return normalizeString.memo[word] =
  6.     [...word.replace(/\s/g, '')]
  7.       .sort() + ''
  8. }
  9. normalizeString.memo = {}
  10.  
  11. const checkAnagram = (word, source) =>
  12.   normalizeString(word) === normalizeString(source)
  13.  
  14.  
  15. // try it out 
  16.  
  17. const target = 'adorn hair'
  18. const anagrams = [
  19.   'hoard rain',
  20.   'hair radon',
  21.   'hadron air',
  22.   'hairdo ran',
  23.   'radian rho',
  24.   'au revoir fail'
  25. ];
  26.  
  27. anagrams.forEach(anigram => { 
  28.   console.log(anigram, '=', checkAnagram(anigram, target))
  29. })

Check if two strings are anagrams of one another.

Save a Text File with JavaScript

  1. function saveFile(text, name = 'file', mime = 'text/plain') {
  2.   const blob = new Blob([
  3.     text
  4.   ], { type: mime });
  5.   const a = document.createElement('a')
  6.  
  7.   a.download = name + '.txt'
  8.   a.href = window.URL.createObjectURL(blob)
  9.   a.click();
  10. }
  11.  
  12. // hacky way to create the button and have it call `saveFile`
  13. // when clicked
  14. document.body.appendChild(
  15.   Object.assign(
  16.     document.createElement`button`,
  17.   { innerText: 'click me' })
  18. ).onclick = () => saveFile('Hello there')

Mangler

  1. const lookupReplacements = {
  2.   // entities (like &sum; or &Theta; - are safer
  3.   // than just putting things inline like ∑ or ®
  4.   'a': ['A', '&Auml;', '∆', '&aacute;'],
  5.   'e': ['&eacute;', '∃'],
  6.   't': ['⊤', '&Tau;'] ,
  7.   'b': ['&beta;'],
  8.   'o': ['&empty;', '_o_']
  9. }
  10.  
  11. function mangle(s) {
  12.   return s.split``.map(letter => {
  13.     const chars = lookupReplacements[letter]
  14.     return chars ? chars[
  15.       Math.floor(
  16.         chars.length * Math.random()
  17.       )
  18.     ] : letter
  19.   }).join``
  20. }
  21.  
  22. document.body.innerHTML = mangle('Been taking a break from making snippets... might start up again...<hr>')
  23.  
  24. document.body.innerHTML += mangle('Been taking a break from making snippets... might start up again...<hr>')
  25.  
  26. document.body.innerHTML += mangle('Been taking a break from making snippets... might start up again...<hr>')

Substitute some characters in a string with some random choices…

snippet.zone ~ 2021-24 /// {s/z}