Skip to content

Instantly share code, notes, and snippets.

View testfirstcoder's full-sized avatar

Andriy Tolstoy testfirstcoder

View GitHub Profile
1 to 1000 - 1 filter(x => x % 3 == 0 || x % 5 == 0) sum
lists:sum([X || X <- lists:seq(1, 1000 - 1), (X rem 3 == 0) or (X rem 5 == 0)]).
[|1..1000 - 1|] |> Array.filter (fun e -> e % 3 = 0 || e % 5 = 0) |> Array.sum
@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
@testfirstcoder
testfirstcoder / HttpActionResultExtentions.cs
Last active December 12, 2018 11:52
Add custom header to IHttpActionResult
public static class HttpActionResultExtentions
{
public static IHttpActionResult AddHeader(this IHttpActionResult result, string name, IEnumerable<string> values)
=> new HeaderActionResult(result, name, values);
private class HeaderActionResult : IHttpActionResult
{
private readonly IHttpActionResult actionResult;
private readonly Tuple<string, IEnumerable<string>> header;
@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 / ModelValidation.cs
Last active March 4, 2016 13:42
Validate model with System.ComponentModel.DataAnnotations
public static class ModelValidation
{
public static IEnumerable<ValidationResult> ValidateModel<T>()
where T : class, new() => ValidateModel(default(T));
public static IEnumerable<ValidationResult> ValidateModel<T>(T model) where T : class, new()
{
model = model ?? new T();
var validationContext = new ValidationContext(model);
@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;
}