I need to remove the first element from a list (the head) and store the value. How would i go about doing that? Im trying to create a stack in sml and making the pop method
In Standard ML lists are equivalent to stacks. You can use hd to get the first element, and you can use tl to get the remaining stack. But hd and tl are partial functions that will fail if the stack is empty. A safer alternative is to use the 'a option type:
fun pop [] = NONE
| pop (top::stack) = SOME (top, stack)
Demonstrating its use:
- pop [1,2,3];
> val it = SOME(1, [2, 3]) : (int * int list) option
It sounds like you are at a level of learning where a tutorial or a textbook better suits your needs.
If you want to remove first element from an array means check below,
HTML
<!DOCTYPE html>
<html data-ng-app="demo">
<head>
<script data-require="angular.js#1.1.5" data-semver="1.1.5" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div data-ng-controller="DemoController">
<ul>
<li data-ng-repeat="item in items">
{{item}}
<button data-ng-click="removeItem($index)">Remove</button>
</li>
</ul>
<input data-ng-model="newItem"><button data-ng-click="addItem(newItem)">Add</button>
</div>
</body>
</html>
Javascript
"use strict";
var demo = angular.module("demo", []);
function DemoController($scope){
$scope.items = [
"potatoes",
"tomatoes",
"flour",
"sugar",
"salt"
];
$scope.addItem = function(item){
$scope.items.push(item);
$scope.newItem = null;
}
$scope.removeItem = function(index){
$scope.items.splice(index, 1);
}
}
Related
I wondered if anyone has managed to get Google maps working within a Django project and if so, could they tell me what I may be doing wrong.
In base.html, I have the following scripts at the end of the body in base.html :
<script src="{% static '/js/script.js' %}" ></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<script async defer src=https://maps.googleapis.com/maps/api/js?key=”API_KEY”&callback=initMap"></script>
<script src="{% static '/js/maps.js' %}" ></script>
Script.js is working fine and map.js is executing but after map.js runs the div element doesn’t display.
In maps.js, I have:
function initMap() {
var map = new google.maps.Map(document.getElementById("map"), {
//zoom: 3,
zoom: 10,
center: {
lat: 46.619261, lng: -33.134766
}
});
var labels = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var locations = [
{ lat: 40.785091, lng: -73.968285},
{ lat: 41.084045, lng: -73.874245},
{ lat: 40.754932, lng: -73.984016}
];
var markers = locations.map(function(location, i){
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
var markerCluster = new MarkerClusterer( map, markers, {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' } );
}
I do not seem to have any Google API key issues. However I have a div in the index.html with the following element and the map is not being displayed:
<div class="row">
<!-- Map -->
<div id="map" class="col-12"></div>
<div class="col-12 mt-2 request-page">
<h2>Map Will Go Above Here </h2>
</div>
</div>
I can get the map to display if I load static in index.html and place the scripts in index.html but ideally I think I am meant to do this in base.html.
I have not changed settings.py, views.py or urls.py to accommodate Google maps, is that where my error is?
Thanks in advance.
if there is a blank gap create a CSS file and add#map {height:500px;width: 100%;}
do not forget to link it to html.
This is probably because the div element #map does not have a defined height. Try adding style="height: 300px" to it.
I'm working on a digital signage module for our intranet. I'm trying to add videos into the mix. I want to play videos first, then play a slide show. I will eventually have this loop. I start by calling the startSequence function, which counts down from the video duration. You can observe the console.log (F12). Then I'm changing the selectedTemplate observable from 1 to 2 , after the videos have played I call the showSlides function, but my view is not detecting the change. Here's a jsFiddle
Do I need a computed observable? -instead of this...
<div>
<!-- ko if: $root.selectedTemplate() == 1 -->
<div data-bind="template: { name: 'videoScript', foreach: $root.dynamicVideos() }"></div>
<!-- /ko -->
<!-- ko if: $root.selectedTemplate() == 2 -->
<div data-bind="template: { name: 'imageScript' }"></div>
<!-- /ko -->
</div>
Currently, you're passing string template names to the template binding.
Fortunately, the binding also supports passing observable strings, or even functions! You can find its documentation on knockout's template page.
Here's an example using a computed template name:
const index = ko.observable(0);
const loop = () => index((index() + 1) % 2);
const activeTemplate = ko.pureComputed(() =>
index() ? "t-slideshow" : "t-video"
);
ko.applyBindings({ activeTemplate });
setInterval(loop, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<div data-bind="template: activeTemplate"></div>
<script type="text/html" id="t-slideshow">
<h2>I'm a slide show! 🖼</h2>
</script>
<script type="text/html" id="t-video">
<h2>I'm a video! 📹</h2>
</script>
I am currently writing an application using socket.io and ionic. I have to handle a list of 6000 people so I've decide to use collection-repeat and swipe + ion-option-button to verify items (remove then from the list)
The names on the list can be removed and the changes will be broadcasted to the rest of apps using sockets so everyone will have their own list updated in real-time
But if If the list change, each row is assigned new data but the html stays the same, including the state of the swiped button!
Here is a screen recording of the bug: https://youtu.be/15oZj7G1DQ0
You can see the list shrinking because another user is removing items from the list and broadcasting to me through websockets, but the button doesn't move along with the item and just stays in the same place.
The problem doesn't happen with ng-repeat but I can't use ng-repeat for this.
And I can't use $ionicListDelegate.closeOptionButtons() to go around the problem because it can be really annoying for the users.
There is any possible solution for this?
resumed code sample:
1) people controller
$rootScope.verify = function(){
this.person.verified = true;
//broadcast to main controller
$rootScope.$broadcast('verify', this.person)
}
2) Main Controller (aka socket controller)
$rootScope.$on('verify', function(e, person) {
//send to socket server
socket.emit('event:verify', person);
});
//incoming data from the socket server
socket.on('event:incoming',function(personData){
var person = $filter('filter')($scope.people, {id: personData.id}, true)[0];
var key = $scope.people.indexOf(person);
$scope.people[key].verified = personData.verified;
});
so if you look at this example: http://play.ionic.io/app/a8d986cdaf19
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<link href="http://code.ionicframework.com/1.0.0/css/ionic.min.css" rel="stylesheet">
<script src="http://code.ionicframework.com/1.0.0/js/ionic.bundle.js"></script>
</head>
<body ng-app="app">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Awesome App</h1>
</ion-header-bar>
<ion-content class="padding">
<ion-list>
<ion-item>
I love kittens!
<ion-option-button ng-if="item" class="button-positive">Share</ion-option-button>
<ion-option-button class="button-assertive">Edit</ion-option-button>
</ion-item>
</ion-list>
<ion-toggle ng-model="item">
Show Delete?
</ion-toggle>
</ion-content>
</ion-pane>
</body>
</html>
You can use ng-if to show or hide a optoin button, so you can broadcast a event when a item is removed and hide the option button with a variable that you can toggle based on the event.
$scope.$emit('objectRemoved');
and then:
$scope.$on('objectedRemoved', function(){
$scope.variableToControlVerifyButton = false;
})
if you post your code I may be able to help more but I hope you get the general idea.
I am trying to introduce Angular in my Django app. I doubt, that my problem is directly correlated with interpolateProvider which is needed because of django templates... but who knows.
I also have a problem with simplified version of that: http://jsfiddle.net/33417xsm/
This is my current version:
<html ng-app="MyApp">
<head>
<script type="text/javascript" src="/static/js/libs/angular/angular.js"></script>
<script type="text/javascript" src="/static/js/app/app.js"></script>
</head>
<body>
<div ng-controller="MyAppController">
[[ 2 + 4 ]]
<p>[[ MyAppController.product.title ]]</p>
</div>
</body>
</html>
file: app.js
(function(){
var app = angular.module('MyApp', []);
app.config(function ($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
}
);
app.controller('MyAppController', function (){
this.product = gem;
});
var gem = {
'title': 'Inferno'
};
})();
My result:
As you can guess, I want to also display Inferno. What I am doing wrong?
Your app config is ok. But i see, you didn't understand clearly angular concept.
You must use $scope to binding data. Also you never need "myController.product" like this notation.
I updated your code http://jsfiddle.net/33417xsm/4/
<div ng-app="MyApp" ng-controller="MyAppController">
{{ 2 + 4 }}
<p>{{product.title}}</p>
</div>
var app = angular.module('MyApp', []);
app.controller('MyAppController', function ($scope){
$scope.product = {"title":"product title"};
});
<script type="text/javascript">
document.getElementById("IDOFELEMENT");
</script>
What is the correct way to turn this into a link?
Can I write
<script type="text/javascript">
document.getElementById("IDOFELEMENT").href("http://www.address.com");
</script>
Many thanks.
javascript:
// this changes the href value<br>
document.getElementById("IDOFELEMENT").href = "http://www.address.com";
and the html:
<a href="www.toBeChanged.com" id="IDOFELEMENT">To Website< /a>
You should specify what kind of element is IDOFELEMENT. But you can't convert it to a link by just adding a href attribute, it only works if IDOFELEMENT
is an hyperlink like <a id="IDOFELEMENT">stuff</a>
Simplest way is to add an onclick event to the element that changes the url to desired address:
<script type="text/javascript">
var element = document.getElementById("IDOFELEMENT");
element.setAttribute('onclick', 'window.location.href=\'http://address.com\'');
</script>
Or if you wanna wrap it with a hyperlink:
<script type="text/javascript">
var element = document.getElementById("IDOFELEMENT");
var parent = element.parentNode;
var link = document.createElement('a');
link.href = 'http://www.address.com';
link.appendChild(element.cloneNode(true));
parent.replaceChild(link, element);
</script>
I hope this helps you.
I came accross the issue - Javascript error: Cannot read property 'parentNode' of null.
I discovered this was due to executing this code while the DOM is not ready.
window.onload solved the issue for me.
<script>
window.onload = function() {
var element = document.getElementById("IDOFELEMENT");
var parent = element.parentNode;
var link = document.createElement('a');
link.href = 'http://www.google.com';
link.appendChild(element.cloneNode(true));
parent.replaceChild(link, element);
};
</script>
You just need to wrap your element in an anchor tag as follows...
<a href="http://www.address.com">
<div id="IDOFELEMENT">
... content ...
</div>
</a>