Skip to content

Instantly share code, notes, and snippets.

@ahafidi
ahafidi / primes.hs
Last active November 2, 2020 08:27
Haskell — Prime numbers
primes = filterPrime [2..]
where filterPrime (p:xs) =
p : filterPrime [x | x <- xs, x `mod` p /= 0]
#include <iostream>
template<int n>
struct facto
{
static constexpr unsigned int value = n * facto<n - 1>::value;
};
template<>
struct facto<0>
#include <iostream>
#include <cstddef>
template<typename T>
struct IsArray
{
static constexpr bool value = false;
};
template<typename T>
const palindrome = str => {
const s = str.toLowerCase().replace(/[\W_]/g, '');
return s === s.split('').reverse().join('');
};
const reverseString = str => [...str].reverse().join('');
/**
*
* @param {string} text
*
* Return a plain JS object providing the number of occurrences of every word containing at least 3 characters from the text
*/
const wordOccurrences = (text) => { // functional programming version
return text
.split(' ')
.filter((token) => token.length > 2)
@ahafidi
ahafidi / tiny_gql_server.js
Created August 2, 2018 13:48
A Tiny GraphQL Server using Apollo Server.
const { ApolloServer, gql } = require('apollo-server')
// The GraphQL schema
const typeDefs = gql`
type Query {
"A simple type for getting started!"
foo: String
foofoo: Int
}
`
@ahafidi
ahafidi / filename.js
Created December 1, 2018 07:05
filename from pathname
const filename = pathname.replace(/^.*[\\\/]/g, '')
@ahafidi
ahafidi / seconds.js
Created December 6, 2018 08:36
Tiny JS snippet to convert time formatted as 'HH:MM:SS' to seconds
const seconds = time => time // formatted as 'HH:MM:SS'
.split(':')
.reverse()
.reduce((acc, val, i) => acc + val * 60 ** i, 0)
import scala.annotation.tailrec
def recursiveSum(f: Int => Int)(a: Int, b: Int): Int = {
if (a > b)
0
else
f(a) + recursiveSum(f)(a + 1, b)
}
def tailRecursiveSum(f: Int => Int)(a: Int, b: Int): Int = {