Skip to content

Instantly share code, notes, and snippets.

View Raagh's full-sized avatar
🎯
Focusing on quality

Patricio Ferraggi Raagh

🎯
Focusing on quality
View GitHub Profile
@Raagh
Raagh / modifyname.js
Created May 4, 2020 12:40
Programacion functional para el dev orientado a objetos parte 2
let john = { name: "John", age: 18 }
john.name = "Patricio"
@Raagh
Raagh / snake.hs
Created April 14, 2020 18:50
Functional snake game - Part 1
module Core where
type Columns = Int
type Rows = Int
type Point = (Rows, Columns)
type Apple = Point
type Snake = [Point]
type Moves = [Move]
data Move = North | South | West | East deriving (Show)
@Raagh
Raagh / index.js
Created April 14, 2020 18:30
Functional Snake Game - Part 2 - final loop
const COLUMNS = 15
const ROWS = 15
const SPEED = 125
let uglyMutableState = initialState
const setupInput = () => {
readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
process.stdin.on("keypress", (str, key) => {
if (key.ctrl && key.name === "c") process.exit()
@Raagh
Raagh / snake.js
Created April 14, 2020 18:29
Functional Snake Game - Part 2 - Step
const step = r.curry((cols, rows, state) =>
r.pipe(nextSnake(cols, rows), nextApple(cols, rows))(state)
)
@Raagh
Raagh / snake.js
Created April 14, 2020 18:27
Functional Snake Game - Part 2 - nextApple
const nextApple = r.curry((cols, rows, state) =>
willEat(r.head(state.snake), state.apple)
? { ...state, apple: point(randomPos(cols), randomPos(rows)) }
: state
)
@Raagh
Raagh / snake.js
Created April 14, 2020 18:27
Functional Snake Game - Part 2 - eat crash
const willEat = r.equals
const willCrash = (cols, rows, state) =>
r.find(r.equals(nextHead(cols, rows, state)))(state.snake)
const nextHead = (cols, rows, { move, snake }) =>
point(
modulo(cols)(r.head(snake).x + move.x),
modulo(rows)(r.head(snake).y + move.y)
)
@Raagh
Raagh / snake.js
Created April 14, 2020 18:26
Functional Snake Game - Part 2 - nextSnake
const nextSnake = r.curry((cols, rows, state) => {
return willCrash(cols, rows, state)
? initialState
: {
...state,
snake: willEat(nextHead(cols, rows, state), state.apple)
? [nextHead(cols, rows, state), ...state.snake]
: [nextHead(cols, rows, state), ...r.dropLast(1, state.snake)],
}
})
@Raagh
Raagh / index.js and snake.js
Created April 14, 2020 18:25
Functional Snake Game - Part 2 - Input
// index.js
const setupInput = () => {
readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
process.stdin.on("keypress", (str, key) => {
if (key.ctrl && key.name === "c") process.exit()
const options = {
UP: addMove(direction.NORTH),
LEFT: addMove(direction.WEST),
@Raagh
Raagh / index.js
Created April 14, 2020 18:24
Functional Snake Game - Part 2
const COLUMNS = 15
const ROWS = 15
const SPEED = 125
let uglyMutableState = initialState
const displayState = display(COLUMNS, ROWS)
const runGameLoop = () => {
setInterval(() => {
displayState(uglyMutableState)
@Raagh
Raagh / ui.js
Created April 14, 2020 18:23
Functional Snake Game - Part 2 - displayWorld
const displayWorld = matrix => {
console.clear()
console.log(intercalate("\r\n", r.map(intercalate(" "), matrix)))
}