Skip to content

Instantly share code, notes, and snippets.

function capitalize(str) {
return str[0].toUpperCase() + str.slice(1)
}
function countdownToChristmas(date) {
const daysLeft = 24 - date
if (daysLeft > 0) {
return `${daysLeft} days till Christmas!`
}
}
const date = new Date().getDate()
countdownToChristmas(date).toUpperCase()
@davidblurton
davidblurton / gist:8520944
Last active January 3, 2016 21:19
Call and apply examples
Array.prototype.max = function() {
return Math.max.apply(Math, this);
};
[1,2,3].max(); // => 3
Array.prototype.all = function() {
return [].every.apply(this, [].slice.call(arguments));
};
@davidblurton
davidblurton / gist:7260533
Created November 1, 2013 03:16
Tried to write merge sort in Clojure. Nearly killed me.
(mergesort [6 4 2 3 5 1])
(defn mergesort [x]
(if (< (count x) 2) x
(let [half (quot (count x) 2)]
(merge (mergesort (take half x)) (mergesort (drop half x))))))
(defn merge[a, b]
(if (empty? a) b
(if (empty? b) a

Keybase proof

I hereby claim:

  • I am davidblurton on github.
  • I am davidblurton (https://keybase.io/davidblurton) on keybase.
  • I have a public key ASBTd-PsNR63x3266ilFt1InXJqgZ5xWK33aqwRct1oGmQo

To claim this, I am signing this object:

@davidblurton
davidblurton / gist:5291687
Created April 2, 2013 11:55
Ookii example
using (FolderBrowserDialog browseDialog = new FolderBrowserDialog())
{
if (browseDialog.ShowDialog() == DialogResult.OK)
{
m_Path.Text = browseDialog.SelectedPath;
}
}
@davidblurton
davidblurton / gist:5116002
Created March 8, 2013 12:01
Allow you to use caller member name attribute in .NET 4. Requires the C#5 compiler so all developers must be using VS2012 else it will fail silently.
// ReSharper disable CheckNamespace
namespace System.Runtime.CompilerServices
// ReSharper restore CheckNamespace
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public sealed class CallerMemberNameAttribute : Attribute
{
}
}
@davidblurton
davidblurton / gist:5115943
Created March 8, 2013 11:53
You can override the default event handlers on certain controls in App.xaml.cs. For example, we put the cursor at the end of the text on textbox gotfocus (the default is at the start) and we select the text in a password box.
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
...
protected override void OnStartup(StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent, new RoutedEventHandler(TextBox_GotFocus));
@davidblurton
davidblurton / gist:5115922
Created March 8, 2013 11:47
Sets the keyboard focus to the first element in the control's tab order.
InitializeComponent();
Loaded += (sender, e) => MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
@davidblurton
davidblurton / gist:5115878
Last active December 14, 2015 16:29
ViewModel is a base class for all view models. It handles INotifyPropertyChanged using the C#5 caller member name attribute
public abstract class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected void RaisePropertyChanged([CallerMemberName] string propertyName = string.Empty)
{
if (propertyName = string.Empty)
{
throw new NotSupportedException("Cannot raise property changed on an empty property name. Make sure you are using the C# 5 compiler to make CallerMemberName work.")