Skip to content

Instantly share code, notes, and snippets.

View tpetrina's full-sized avatar
🐜
ant is a hard-working bug. be like ant queen - spawn more workers!

Toni Petrina tpetrina

🐜
ant is a hard-working bug. be like ant queen - spawn more workers!
View GitHub Profile
@tpetrina
tpetrina / CompilerServicesExtensions
Created March 30, 2012 19:45
Attribute types definitions for using C# 5 caller info attributes in projects targeting earlier frameworks i.e. before .NET 4.5
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
public sealed class CallerMemberNameAttribute : Attribute
{
public CallerMemberNameAttribute()
{ }
}
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
@tpetrina
tpetrina / DataServiceAsyncExtensions
Created April 1, 2012 17:28
Wrappers for asynchronous operations exposed by DataServiceQuery<T> and DataServiceContext
// Use in VS11 and .NET 4.5 or in VS10 with the Async CTP installed
//
namespace DataServiceAsyncExtensions
{
using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Threading.Tasks;
@tpetrina
tpetrina / DirectXHelper.h
Created July 30, 2012 21:02
Asynchronous file loading for Metro styled applications using DirectX/C++
inline Concurrency::task<Platform::String^> ReadStringAsync(Platform::String^ filename)
{
using namespace Windows::Storage;
using namespace Concurrency;
auto folder = Windows::ApplicationModel::Package::Current->InstalledLocation;
task<StorageFile^> getFileTask(folder->GetFileAsync(filename));
auto readBufferTask = getFileTask.then([] (StorageFile^ f)
@tpetrina
tpetrina / extensions.cs
Created September 13, 2012 12:48
Similar to SelectMany method, only projects members
public static IEnumerable<U> SelectProjections<T, U>(this IEnumerable<T> @this, params Func<T, U>[] projections)
{
if (@this == null)
throw new NullReferenceException();
if (projections == null || !projections.Any())
yield break;
foreach (var item in @this)
{
foreach (var projection in projections)
@tpetrina
tpetrina / HashSet.cs
Created January 20, 2013 19:47
Naive implementation of HashSet<T> using Dictionary<T, bool> for Windows Phone 7.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Common
{
public interface ISet<T> : ICollection<T>, IEnumerable<T>, IEnumerable
{
bool Add(T item);
@tpetrina
tpetrina / .xaml
Created September 2, 2015 07:47
FloatingTextBoxStyle
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="FloatingHeaderTextBoxStyle" TargetType="TextBox">
<Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}"/>
<Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}"/>
<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
<Setter Property="Background" Value="{ThemeResource SystemControlBackgroundAltHighBrush}"/>
<Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundChromeDisabledLowBrush}"/>
@tpetrina
tpetrina / medium-react-register.html
Last active September 26, 2017 14:22
React form complication 1
<div>
<h1>Register</h1>
<div>
<label>Username</label>
<input type="text" name="username" />
</div>
<div>
<label>Password</label>
<input type="password" name="password" />
</div>
@tpetrina
tpetrina / register.jsx
Last active September 26, 2017 14:37
React form complication 2
const RegisterForm = ({
username, password, isRegistering,
onRegister, set
}) => (
<div>
<h1>Register</h1>
<div>
<label>Username</label>
<input type="text" name="username" value={username} onChange={set}
/>
@tpetrina
tpetrina / state.js
Last active September 26, 2017 15:20
React form complication 3
let registration = {
username: "",
password: "",
isRegistering: false,
onRegister: r => {
// login(r.username, r.password).then()...
r.setState({ isRegistering: true })
setTimeout(() => {
r.setState({ isRegistering: false })
@tpetrina
tpetrina / wrap.js
Last active September 26, 2017 14:46
React form complication 4
export const wrap = (state, WrappedComponent) =>
class extends React.Component {
foos = {}
state = state || {}
set = e => {
this.setState({ [e.target.name]: e.target.value })
};
componentWillMount() {