Skip to content

Instantly share code, notes, and snippets.

View NathanSass's full-sized avatar
🐬

Nathan Sass NathanSass

🐬
View GitHub Profile
@NathanSass
NathanSass / TypeAheadTest.kt
Last active August 5, 2021 11:23
A basic type ahead
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.transform
import kotlinx.coroutines.flow.transformLatest
import kotlinx.coroutines.runBlocking
import org.junit.Test
private suspend fun handleTypeAhead(dataSource: Flow<String>, onChange: (results: List<String>) -> Unit) {
// if a user is typing quickly wait for a pause before collecting most recent update. Ignore anything less then 3 characters.
val resultsFromUser = dataSource.debounce(500).filter { it.length > 3 }
// make an api call to get results for each user's query
// transformLatest will cancel an older api call if a newer arrives first
// this ensures a user will not see typeahead results for an out of date query
val apiCalls = resultsFromUser.transformLatest {
emit(getResultsAsync(it))
@NathanSass
NathanSass / rxjava2.kt
Created December 27, 2018 17:33
Example of a fancy zip chain
override fun getDocumentAndAudiobook(docId: Int): Single<AudioData.LoaderResult> {
val licenseRequest = audioApi.getLicense(docId).toObservable()
val documentRequest = documentLoaderObservable(docId)
val requests = mutableListOf(licenseRequest, documentRequest)
return Observable.zip(requests,
Function<Array<Any>, CombinedResult> {
return@Function resolve(it)
})
.flatMap(Function<CombinedResult, Observable<Pair<ScribdDocument, FindawayApi.PlayListResponse>>> {
val playlistRequest = audioApi.getPlaylist(
@NathanSass
NathanSass / 0.2.1-boggle_class_from_methods.rb
Last active January 2, 2016 23:09 — forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
phase 0 unit 2 week 1 boggle class challenge
class Drawer
attr_reader :contents
# Are there any more methods needed in this class?
def initialize
@contents = []
@open = true
end
first_name = "Nathan"
last_name = "Sass"
age = 24
@NathanSass
NathanSass / 0_reuse_code.js
Created March 11, 2014 16:48
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@NathanSass
NathanSass / jquery_example.html
Last active August 29, 2015 13:56 — forked from dbc-challenges/jquery_example.html
Intro to jQuery for Phase 0
<!DOCTYPE html>
<html>
<head>
<title>DOM manipulation with jQuery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery_example.js"></script>
</head>