knowthen / fpjs Goto Github PK
View Code? Open in Web Editor NEWCompanion repo for Functional Programming for Beginners with Javascript
Home Page: https://courses.knowthen.com/courses/functional-programming-for-beginners-with-javascript
Companion repo for Functional Programming for Beginners with Javascript
Home Page: https://courses.knowthen.com/courses/functional-programming-for-beginners-with-javascript
I was wondering how you'd implement an init
function instead of initModel
. How would we make an HTTP request that populates the initial data?
Like this in Elm:
init : (Model, Cmd Msg)
init = (initModel, getData)
main : Program Never
main = App.program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
How would the Javascript look like? I can't wrap my head around it.
function app(initModel, update, view, node) {
// The initial model is set as the current model.
let model = initModel;
// The new view is rendered based on the initial model.
let currentView = view(dispatch, model);
// The new view is created and appended to the DOM.
let rootNode = createElement(currentView);
node.appendChild(rootNode);
// Dispatch sends a message with a type and payload.
function dispatch(msg) {
// Update "catches" it, updates the current model, and returns it.
// returns either a model or an array with model and command
const updates = update(msg, model);
// Array check boolean
const isArray = updates.constructor === Array;
// Get the model from the array. If not array, it's just the model.
model = isArray ? updates[0] : updates;
// Get the command form the array
const command = isArray ? updates[1] : null;
// Pass the dispatch function and command object for HTTP execution
httpEffects(dispatch, command);
// Return the new view either from normal types, or HTTP ones
// The new view is rendered based on the returned new model.
const updatedView = view(dispatch, model);
// The DOM updates are defined.
const patches = diff(currentView, updatedView);
// The DOM updates are applied.
rootNode = patch(rootNode, patches);
// The new view becomes the old view in for future dispatches.
currentView = updatedView;
}
function httpEffects(dispatch, command) {
if (command === null) {
return;
}
// Get the request object from the command.
let request = command.request;
// Fetch, and dispatch a success or fail message.
fetch(request.url, request.headers, request.body)
.then(res => res.json())
.then(result => {
// Dispatch the returned message defined in the command.
dispatch(command.successMsg(result));
})
.catch(error => {
// Dispatch the returned message defined in the command.
dispatch(command.errorMsg(error));
});
}
}
// App initializing.
const rootNode = document.getElementById('app');
app(initModel, update, view, rootNode);
Hello,
when following the instructions on knowthen/fpjs-setup I get an error (tried git clone several times, getting a slightly different error every time):
user@VirtualBox:~/functional_programming$ git clone [email protected]:knowthen/fpjs.git
Cloning into 'fpjs'...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
user@VirtualBox:/functional_programming$ git clone [email protected]/knowthen/fpjs.git
fatal: repository '[email protected]/knowthen/fpjs.git' does not exist
user@VirtualBox:~/functional_programming$ git clone [email protected]:knowthen/fpjs.git
Cloning into 'fpjs'...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Doing git clone https://github.com/knowthen/fpjs.git works just fine.
Cloning into 'fpjs'...
remote: Counting objects: 246, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 246 (delta 1), reused 3 (delta 0), pack-reused 237
Receiving objects: 100% (246/246), 102.50 KiB | 0 bytes/s, done.
Resolving deltas: 100% (125/125), done.
Checking connectivity... done.
Thought I report it in case somebody else bumps to this error. Not sure what the cause is though.
when I run nvm setup I'm getting errors:
node-pre-gyp ERR! Tried to download(404): https://fsevents-binaries.s3-us-west-
2.amazonaws.com/v1.1.3/fse-v1.1.3-node-v64-darwin-x64.tar.gz
node-pre-gyp ERR! Pre-built binaries not found for [email protected] and [email protected] (node-v64 ABI, unknown) (falling back to source compile with node-gyp)
node-pre-gyp ERR! Tried to download(undefined): https://fsevents-binaries.s3-us-west-
2.amazonaws.com/v1.1.3/fse-v1.1.3-node-v64-darwin-x64.tar.gz
node-pre-gyp ERR! Pre-built binaries not found for [email protected] and [email protected] (node-v64 ABI, unknown) (falling back to source compile with node-gyp)
SOLINK_MODULE(target) Release/.node
SOLINK_MODULE(target) Release/.node
I'm installing on an iMac Pro running Mojave. Any thoughts on how to correct?
best,
-james
I've attached the log below
setupIssues.txt
Extremly beautiful designed Calorie counter app - a simple app but beautiful application architecture!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.