Skip to content

Instantly share code, notes, and snippets.

View AhmedMourad0's full-sized avatar
🐧
Farming Penguins

Ahmed Mourad AhmedMourad0

🐧
Farming Penguins
View GitHub Profile
@AhmedMourad0
AhmedMourad0 / value-based-copy-example.kt
Last active September 4, 2020 00:39
Code snippets for the `value-based classes and error-handling` Medium article.
val password = Password.of("Some34Really434Hard21Password!").orThrow() //Throw on violation just for demonstration
val haha = password.copy(value = "gotcha")
@AhmedMourad0
AhmedMourad0 / value-based-adt-list-example.kt
Created August 12, 2020 17:58
Code snippets for the `value-based classes and error-handling` Medium article.
data class Password private constructor(val value: String) {
companion object {
fun of(value: String): Either<List<Violation>, Password> {
val violations = buildList {
if (value.isTooShort()) { add(Violation.PasswordTooShort(MIN_LENGTH)) }
if (value.containsNoNumbers()) { add(Violation.PasswordContainsNoNumbers) }
}
return if (violations.isEmpty()) Password(value).right() else violations.left()
}
}
@AhmedMourad0
AhmedMourad0 / value-based-exceptions-example.kt
Created August 12, 2020 01:47
Code snippets for the `value-based classes and error-handling` Medium article.
data class Password private constructor(val value: String) {
companion object {
fun of(value: String): Password {
return when {
value.isTooShort() -> throw PasswordTooShortException()
value.containsNoNumbers() -> throw PasswordContainsNoNumbersException()
else -> Password(value)
}
}
}
@AhmedMourad0
AhmedMourad0 / value-based-adt-example.kt
Last active August 12, 2020 17:48
Code snippets for the `value-based classes and error-handling` Medium article.
data class Password private constructor(val value: String) {
companion object {
fun of(value: String): Either<Violation, Password> {
return when {
value.isTooShort() -> Violation.PasswordTooShort(MIN_LENGTH).left()
value.containsNoNumbers() -> Violation.PasswordContainsNoNumbers.left()
else -> Password(value).right()
}
}
}
@AhmedMourad0
AhmedMourad0 / basic-value-based-example.kt
Last active September 10, 2020 00:05
Code snippets for the `value-based classes and error-handling` Medium article.
data class Password private constructor(val value: String) {
companion object {
fun of(value: String): Password? {
return if (value.isAGoodPassword()) Password(value) else null
}
}
}
@AhmedMourad0
AhmedMourad0 / InfixToPrefixConverter.py
Last active November 10, 2019 19:48
For college.
from Stack import Stack
def is_operand(token):
"""
checks if token consists completely of digits or letters
:return: true if token is either all digit or all letters (case insensitive), false otherwise
"""
return token.isalpha() or token.isdigit()
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
#include <iostream>
#include <cmath>
using namespace std;
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; ++i)
@AhmedMourad0
AhmedMourad0 / classification.py
Last active October 28, 2018 14:24
A college thing.
def bubble_sort(tuples):
for iteration in range(len(tuples) - 1, 0, -1):
for i in range(iteration):
if tuples[i][1] > tuples[i + 1][1]:
temp = tuples[i]
tuples[i] = tuples[i + 1]
tuples[i + 1] = temp
return tuples
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />