Skip to content

Instantly share code, notes, and snippets.

View MarcoNicolodi's full-sized avatar

Marco Nicolodi MarcoNicolodi

View GitHub Profile
(ns advent-of-code-2020.day4
(:require [clojure.string :as str]
[clojure.set :as set]))
(def path "./resources/day4-input")
(def required-data
#{"byr" "iyr" "eyr" "hgt" "hcl" "ecl" "pid"})
(defn valid-byr? [byr]
(ns advent-of-code-2020.day3
(:require [clojure.string :as str]))
(def path "./resources/day3-input")
(defn wire-vec->map [map-vec]
{:path map-vec
:width (-> map-vec first count)
:height (count map-vec)})
(ns advent-of-code-2020.day2
(:require [clojure.string :as str]))
;;part 1
(def path "./resources/day2-input")
(defn wire->policy+password [path]
(->> path
slurp
str/split-lines
@MarcoNicolodi
MarcoNicolodi / day_3_part_1.clj
Created December 8, 2019 22:48
advent of code day 3 part 1
(ns advent-of-code-2019.day-3
(:require [clojure.string :as str]
[clojure.set :as set]))
(def letter->direction {"R" :right "L" :left "U" :up "D" :down})
(defn str->direction+distance [[dir & dists]]
{:direction (letter->direction (str dir))
:distance (Integer/parseInt (str/join dists))})
@MarcoNicolodi
MarcoNicolodi / day_2.clj
Last active December 7, 2019 17:02
advent of code 2019 day 2
(ns advent-of-code-2019.day-2
(:require [clojure.string :as str]))
(defn halt? [[op & _]] (= op :halt))
(defn pointer+memory->instruction [pointer memory]
(let [int->opcode {1 + 2 * 99 :halt}
op (nth memory pointer)
in1 (nth memory (inc pointer) 0)
in2 (nth memory (+ 2 pointer) 0)
@MarcoNicolodi
MarcoNicolodi / Proprosal.cs
Created November 29, 2019 16:27
Showing different relationships between aggregates
public class Proposal : Entity
{
public Guid CandidateId { get; }
public Guid JobListingId { get; }
}
public class Proposal : Entity
{
public Candidate CandidateId { get; }
public JobListing JobListingId { get; }
@MarcoNicolodi
MarcoNicolodi / Proposal.cs
Created November 29, 2019 16:02
Proposal aggregate pseudo code showing its integrity with comments
public class Proposal
{
private List<Comments> _comments;
public IReadOnlyList<Comments> Comments => _comments.AsReadOnly();
private bool IsOpenForDiscussion => Status != ProposalStatus.Pending;
public void Discuss(Guid userId, string content)
{
if(!IsOpenForDiscussion)
throw new DomainException("Can't discuss about an closed proposal");
@MarcoNicolodi
MarcoNicolodi / accept-proposal.js
Last active October 15, 2019 10:11
Anemic domain model work better for functional style programming;
const acceptProposal => (id, db, bus, logger) => {
const proposal = db.get(id);
proposal.acceptedAt = moment.now();
proposal.status = ACCEPTED;
db.save(proposal);
bus.send({ type: ACCEPTED, payload: proposal.id});
logger.sucess("Proposal accepted");
}
@MarcoNicolodi
MarcoNicolodi / ProposalService.cs
Created October 15, 2019 01:26
Proposal service with anemic domain, and no domain service
public class ProposalService
{
public void AcceptProposal(Guid proposalId)
{
var proposal = _repository.Find(proposalId);
//calling Accept method instead of changing its properties
if(DateTime.Now.Substract(proprosal.SentAt).TotalDays > 15)
throw new DomainException("This proprosal has expired");
@MarcoNicolodi
MarcoNicolodi / ProposalService.cs
Created October 15, 2019 01:13
Proposal service passing domain service to proposal Accept method as argument
public class ProposalService
{
public void AcceptProposal(Guid proposalId)
{
var proposal = _repository.Find(proposalId);
proposal.Accept(domainService);
_repository.Save(proposal);