Skip to content

Instantly share code, notes, and snippets.

Chinmay Naik chinmay185

Block or report user

Report or block chinmay185

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 grep_parallel_test.go
package grep
import (
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
@chinmay185
chinmay185 / grep_parallel.go
Last active Apr 14, 2017
basic simulation of grep command using multiple goroutines (for tests, see https://gist.github.com/chinmay185/15e0a81deee73571ce9b74d65d1da7eb)
View grep_parallel.go
package grep
import (
"bufio"
"flag"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@chinmay185
chinmay185 / josephus_using_channels_goroutines.go
Created Mar 25, 2017
Simulates Josephus problem solution using Goroutines and Channels
View josephus_using_channels_goroutines.go
package main
import (
"fmt"
"time"
)
type Command struct {
message string
senderId int
@chinmay185
chinmay185 / gogrep.go
Created Mar 25, 2017
Grep like command implementation in Go
View gogrep.go
package main
import (
"io/ioutil"
"os"
"log"
"bufio"
"fmt"
"strings"
"path/filepath"
@chinmay185
chinmay185 / async-map-reduce.js
Last active Aug 29, 2015
Demonstration of async map reduce using promises
View async-map-reduce.js
var Promise = require("bluebird");
var fileParts = ["http://myfiles.com/file1/part1",
"http://myfiles.com/file1/part2",
"http://myfiles.com/file1/part3"];
var fetchPart = function(partUrl) {
// returns the promise of the part of a file
};
var filePartPromises = fileParts.map(fetchPart);
@chinmay185
chinmay185 / parallel-async.js
Created Apr 27, 2015
using map to start multiple async operations at the same time
View parallel-async.js
var Promise = require("bluebird");
var productIds = ["productId1", "productId2", "productId3"];
var getProductsFromDb = function(productId) {
// returns a promise of product
}
var productPromises = productIds.map(getProductFromDb);
Promise.all(productPromises).then(function(products) {
@chinmay185
chinmay185 / sequential-async.js
Last active Aug 29, 2015
Run async operation for each value in a collection where each async operation is executed only when the previous one completes.
View sequential-async.js
var Promise = require("bluebird");
var apiUrls = ["url1", "url2", "url3"];
var request = function(url) {
// request the rate limited api (returns a promise)
};
var saveResponse = function(response) {
// save response to file/db (returns a promise)
};
@chinmay185
chinmay185 / deferred-fixed.js
Last active Aug 29, 2015
getting rid of unnecessary deferred
View deferred-fixed.js
var getUserEmail = function (username) {
return userRepository.find(username).then(function (user) {
return user.email;
});
};
getUserEmail("emma").then(console.log);
@chinmay185
chinmay185 / deferred-abused.js
Last active Aug 29, 2015
unnecessary use of deferred.
View deferred-abused.js
var getUserEmail = function(username) {
var deferred = q.defer();
userRepository.find(username) // assume this returns a promise
.then(function(user){
deferred.resolve(user.email);
});
return deferred.promise;
};
getUserEmail("emma").then(console.log);
@chinmay185
chinmay185 / missing-return.js
Created Apr 13, 2015
missing return breaks the promise chain and causes subtle bugs
View missing-return.js
var q = require("q");
var getUser = function(username) {
return q.delay(500).then(function(){
return username + " user";
});
};
var getTweets = function(user) {
return q.delay(500).then(function(){
You can’t perform that action at this time.