Skip to content

Instantly share code, notes, and snippets.

Daniele Campogiani dcampogiani

Block or report user

Report or block dcampogiani

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View FizzBuzz.kt
fun fizzBuzz(input: Int, entryList: List<Entry>): String {
val filtered = entryList.filter { input isMultipleOf it.value }
return if (filtered.isEmpty()) {
input.toString()
} else {
filtered.joinToString(separator = "") { it.decorateString }
}
}
infix fun Int.isMultipleOf(other: Int) = this.rem(other) == 0
View ValidatedNelValidationErrorActivity.kt
private fun handleValid(data: Data) {
val message = "Mail: ${data.mail} Phone: ${data.phone}"
showMessage(message)
}
View ValidatedNelValidationErrorActivity.kt
fun handleInvalid(errors: Nel<ValidationError>) {
errors.map { handleInvalidField(it) }.all.forEach { it.second.error = it.first }
}
fun handleInvalidField(validationError: ValidationError): Pair<String, TextInputLayout> =
when (validationError) {
ValidationError.InvalidMail -> Pair(getString(R.string.invalid_mail), emailWrapper)
ValidationError.InvalidPhoneNumber -> Pair(getString(R.string.invalid_phone_number), numberWrapper)
}
View ValidatedNelValidationErrorActivity.kt
class ValidatedNelValidationErrorActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_input)
val viewModel = ViewModelProviders.of(this)[ValidatedNelValidationErrorViewModel::class.java]
signup_button.setOnClickListener {
viewModel.validate(email.text.toString(), number.text.toString())
View ValidatedNelValidationErrorViewModel.kt
class ValidatedNelValidationErrorViewModel : ViewModel() {
private val mutableValidation: MutableLiveData<Validated<Nel<ValidationError>, Data>> = MutableLiveData()
val validation: LiveData<Validated<Nel<ValidationError>, Data>>
get() = mutableValidation
fun validate(mail: String, phoneNumber: String) {
mutableValidation.value = validateData(mail, phoneNumber)
View ValidatedMap2.kt
fun validateData(mail: String, phoneNumber: String): Validated<Nel<String>, Data> {
return Validated.applicative<Nel<ValidationError>>(Nel.semigroup()).map(mail.validatedMail(), phoneNumber.validatedPhoneNumber()) {
Data(it.a, it.b)
}.fix()
}
View ValidatedExtensions.kt
fun String.validatedMail(): Validated<Nel<ValidationError>, String> =
when {
validMail(this) -> this.valid()
else -> ValidationError.InvalidMail.nel().invalid()
}
fun String.validatedPhoneNumber(): Validated<Nel<ValidationError>, String> =
when {
validNumber(this) -> this.valid()
else -> ValidationError.InvalidPhoneNumber.nel().invalid()
View ValidationError.kt
sealed class ValidationError {
object InvalidMail : ValidationError()
object InvalidPhoneNumber : ValidationError()
}
View EitherActivity.kt
private fun handleRight(data: Data) {
val message = "Mail: ${data.mail} Phone: ${data.phone}"
showMessage(message)
}
View EitherActivity.kt
private fun handleLeft(error: String) {
showMessage(error)
}
You can’t perform that action at this time.