Skip to content

Instantly share code, notes, and snippets.

View testfirstcoder's full-sized avatar

Andriy Tolstoy testfirstcoder

View GitHub Profile
lists:sum([X || X <- lists:seq(1, 1000 - 1), (X rem 3 == 0) or (X rem 5 == 0)]).
@testfirstcoder
testfirstcoder / Problem 1 - Multiples of 3 and 5.exs
Last active November 12, 2015 14:26
ProjectEuler in Elixir
1..1000 - 1 |> Enum.filter(fn x -> (rem x, 3) == 0 || (rem x, 5) == 0 end) |> Enum.sum
[|1..1000 - 1|] |> Array.filter (fun e -> e % 3 = 0 || e % 5 = 0) |> Array.sum
1 to 1000 - 1 filter(x => x % 3 == 0 || x % 5 == 0) sum
@testfirstcoder
testfirstcoder / UriHelpers.cs
Last active February 23, 2016 11:38
Uri concatenation
public static class UriHelpers
{
public static Uri Concat(this Uri baseUri, string relativeUri)
{
const char separator = '/';
return new Uri(new Uri(baseUri.AbsoluteUri.TrimEnd(separator) + separator), relativeUri.TrimStart(separator));
}
}
@testfirstcoder
testfirstcoder / FunctionalTry.cs
Last active February 24, 2016 08:58
Functional try/catch/finally
public static class FunctionalTry
{
public static T Try<T>(Func<T> expression, Func<Exception, T> onError)
=> Try(expression, onError, () => { });
public static void Try(Action action, Action<Exception> onError)
=> Try(() => Do(action), ex => Do(() => onError(ex)));
public static T Try<T>(Func<T> expression, Func<Exception, T> onError, Action onDone)
{
@testfirstcoder
testfirstcoder / FunctionalUse.cs
Created February 24, 2016 09:09
Functional using
public static class FunctionalUse
{
public static T Use<TDisposable, T>(Func<TDisposable> disposable, Func<TDisposable, T> expression)
where TDisposable : IDisposable
{
using (var client = disposable())
return expression(client);
}
public static void Use<TDisposable>(Func<TDisposable> disposable, Action<TDisposable> action)
@testfirstcoder
testfirstcoder / StringLengthAttributeValue.cs
Created February 26, 2016 11:58
Get value from DataAnnotations.StringLengthAttribute
int GetStringLengthAttributeValue<T>(string property)
{
return typeof(T)
.GetProperty(property)
.GetCustomAttribute<StringLengthAttribute>()
.MaximumLength;
}
@testfirstcoder
testfirstcoder / RegularExpressionAttributePattern.cs
Created February 26, 2016 13:31
Get pattern from DataAnnotations.RegularExpressionAttribute
string GetRegularExpressionAttributePattern<T>(string property)
{
return typeof(T)
.GetProperty(property)
.GetCustomAttribute<RegularExpressionAttribute>()
.Pattern;
}
@testfirstcoder
testfirstcoder / HttpRequestMessageHelpers.cs
Created March 2, 2016 15:16
Request root path (host path + virtual path)
public static class HttpRequestMessageHelpers
{
private const char separator = '/';
public static string GetRootPath(this HttpRequestMessage message)
=> GetHostPath(message) + separator + GetVirtualPath(message);
private static string GetHostPath(HttpRequestMessage message)
=> message.RequestUri.GetLeftPart(UriPartial.Authority).TrimEnd(separator);