Skip to content

Instantly share code, notes, and snippets.

View freewayz's full-sized avatar
🏠
Working from home

Peter Edache freewayz

🏠
Working from home
View GitHub Profile
@getify
getify / 1.js
Last active September 29, 2021 11:58
experiment: mimicking React's new "useState()" hook for stand-alone functions, including "custom hooks"
"use strict";
[foo,bar] = TNG(foo,bar);
// NOTE: intentionally not TNG(..) wrapping useBaz(), so that it's
// basically like a "custom hook" that can be called only from other
// TNG-wrapped functions
function foo(origX,origY) {
var [x,setX] = useState(origX);
var [y,setY] = useState(origY);
@freewayz
freewayz / flatten_array.py
Created February 12, 2017 23:59
Python implementation of flatten an array
def flatten_array(arr):
output = []
for val in arr:
if type(val) == list: # is the current value we are looking at is also a list
output.extend(flatten_array(val)) # then recursive call itself to start from
# the beginning and use python list extend
else:
output.append(val) # ok this is not a list just append to the bottom
return output
@bentappin
bentappin / models.py
Last active April 26, 2022 11:13
Checking if an object is a foreign key anywhere in Django.
from django.db import models
class Thing(models.Model):
# [ snip ]
def is_deletable(self):
for rel in self._meta.get_all_related_objects():
if rel.model.objects.filter(**{rel.field.name: self}).exists():
return False
return True