Skip to content

Instantly share code, notes, and snippets.

View newmanbrad's full-sized avatar

Brad Newman newmanbrad

View GitHub Profile
@newmanbrad
newmanbrad / myprojectexample.csproj
Created August 13, 2018 15:02
Adding JavaScript tasks to your MSBuild project.
<!-- custom targets -->
<Target Name="BeforeBuild">
<Message Text="Node Check Started" />
<Exec Command="node || start http://nodejs.org &amp;&amp; echo You need to install NodeJS" ContinueOnError="false" />
<Message Text="Node Check Ended" />
<Message Text="NPM Install Started" />
<Exec WorkingDirectory="$(ProjectDir)" Command="npm install" ContinueOnError="false" />
<Message Text="NPM Install Ended" />
<div class="brads-element">
<p>{{counter}}</p>
<button (click)="increase()">Increase</button>
<button (click)="descrease()">Decrease</button>
</div>
// Safari, in Private Browsing Mode, looks like it supports localStorage but all calls to setItem
// throw QuotaExceededError. We're going to detect this and just silently drop any calls to setItem
// to avoid the entire page breaking, without having to do a check at each usage of Storage.
if (typeof window.localStorage === 'object') {
try {
window.localStorage.setItem('localStorage', 1);
window.localStorage.removeItem('localStorage');
} catch (e) {
(function () {
var Storage = function (type) {
@newmanbrad
newmanbrad / sideEffectExample.js
Created January 6, 2017 15:47
An example of a side effect that makes a program hard to reason about
// The result of the 'exampleFunction' is difficult to reason about.
let n = 10;
// function with a side effect
const exampleFunction = function ( i ) {
return n = n + i;
};
exampleFunction( 2 ); // result is 12
@newmanbrad
newmanbrad / declarativeExample.js
Last active January 6, 2017 17:27
Example of declarative style
/**
Decalartive style example of a program that will find the average grade for classes with
more than one student enrolled.
**/
let courses = [
{ name: 'functional fundamentals', enrolled: 2, grade: 100, students: 30 },
{ name: 'category theory', enrolled: 2, grade: 80, students: 25 },
{ name: 'basket weaving', enrolled: 1, grade: 89, students: 43 }
];
@newmanbrad
newmanbrad / palindromeChainLength.js
Created December 29, 2016 14:29
Puzzle Question and answer.
/***
Number is a palindrome if it is equal to the number with digits in reversed order. For example, 5, 44, 171, 4884 are palindromes and 43, 194, 4773 are not palindromes.
Write a method palindrome_chain_length which takes a positive number and returns the number of special steps needed to obtain a palindrome. The special step is: "reverse the digits, and add to the original number". If the resulting number is not a palindrome, repeat the procedure with the sum until the resulting number is a palindrome.
If the input number is already a palindrome, the number of steps is 0.
Input will always be a positive integer.
@newmanbrad
newmanbrad / imperativeStyle.js
Last active January 6, 2017 17:28
Imperative Code Sample #1
/**
Imperative style example of a program that will find the average grade for classes with
more than one student enrolled.
**/
let courses = [
{ name: 'functional fundamentals', enrolled: 2, grade: 100, students: 30 },
{ name: 'category theory', enrolled: 2, grade: 80, students: 25 },
{ name: 'basket weaving', enrolled: 1, grade: 89, students: 43 }
];
@newmanbrad
newmanbrad / functionalTypescript1.ts
Last active December 27, 2016 19:15
functional Typescript Examples #1
let courses = [
{ name: 'functional fundamentals', enrolled: 2, grade: 100, students: 30 },
{ name: 'category theory', enrolled: 2, grade: 80, students: 25 },
{ name: 'basket weaving', enrolled: 1, grade: 89, students: 43 }
];
function avg(nums: number[]): number {
const total = nums.reduce((a,b) => a + b, 0);
return (nums.length == 0) ? 0 : total / nums.length;
}
@newmanbrad
newmanbrad / arrayOfSubStringsinArray.js
Created December 21, 2016 16:29
Check if sub-strings are in an array of strings.
function inArray(arr1, arr2) {
return arr1.filter(function(needle) {
return arr2.some(function(haystack) {
return haystack.indexOf(needle) > -1;
});
}).sort();
}
@newmanbrad
newmanbrad / composeFunctions.js
Created December 9, 2016 16:35
Composing Functions
var compose = function(f, g) {
return function(x) {
return f(g(x));
};
};